-1

I am currently joining a new project in C++, but I am only acquainted with C. I will look to learn the STL and BOOST, but in the meantime, I foresee myself only programming in the C subset of C++.

My question is, when will just using the C subset of C++ and compiling with the C++ compiler be notably worse than just compiling with a C compiler?

I ask mostly in performance (things like size of executable are not a concern, as we are not working in embedded systems).

Alex Coleman
  • 607
  • 1
  • 4
  • 11
  • 3
    Most times people working in C++ that come from C carry bad habits; C++ is a completely different language. Do get a good book and forget what you know or assume so far. – Michael Chourdakis Jun 13 '20 at 15:43
  • This is pretty broad, and of course it will depend on your compiler. But if you're using a compiler that uses a common back-end for C and C++ (e.g. gcc, clang), then I would expect that any code in the subset, which actually does the same thing in both languages, would tend to be compiled the same. If not then I'd consider it a (minor) compiler bug. Of course, there are instances where the same code behaves *differently* in C++ than in C, and in such cases the performance may very well be different. – Nate Eldredge Jun 13 '20 at 15:45
  • From a non performance related view. There are certain things that will be frowned upon if you're writing pure `C` in a `C++` project. For example, manual memory management, type punning, `C` style casts. That'll make `C++` developers cringe in most scenarios (or might be undefined behaviour). Take some time to learn the basics of `C++`. I recommend pretty much any course by Kate Gregory on PluralSight. – WBuck Jun 13 '20 at 16:03
  • 1
    Some C idioms do not work in C++. C++ originally had a few C incompatibilities. Since then, the languages have grown further apart. The C standardization committee does not feel beholden to C++ to guide or constrain the evolution of C. (I agree with their position.) – Eljay Jun 13 '20 at 16:13
  • @WBuck I find myself using C-style casts in C++ quite often just because they're less verbose, even though I know the C++ casts are more type-safe and versatile. Certainly there's nothing in C to substitute for `dynamic_cast`. – Mark Ransom Jun 14 '20 at 04:26
  • The main risk is of attracting condescending remarks from purists – M.M Jun 14 '20 at 23:18

3 Answers3

1

Using the common subset of C and C++ is good and in fact necessary, when writing a header that is designed to be usable from both C and C++.

Limiting oneself to C conforming code when writing a C++ source file, and correspondingly limiting oneself to C++ conforming code when writing C source file is unnecessary and leads to code that would be considered poor quality by majority of programmers.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

I ask mostly in performance

I would say that this is the wrong metric to use. Many C++ compilers share their codebase with C compilers. That is, if a given block of code is valid C and valid C++ (with the same semantics in both languages), the object code produced by the C compiler is likely to be comparable to – if not the same as – that produced by the C++ compiler (ignoring C++ name mangling).

A better metric for "bad" is robustness. The differences between C and C++ are more than syntax. C++ is object-oriented, using a different mindset than C. Given a piece of code that happens to be valid in both languages, the style will often reveal which language it was written as. Thanks to object-orientation, C++ code promotes more robust practices than exist in C (whereas C has a better focus on raw speed). Perhaps the simplest such practice to grasp is RAII.

So the question of "bad" should not focus on the object code, but on the source code. What will your colleagues on this project think of your coding style? You might be better served by learning C++ philosophy before worrying about learning all of the Boost and STL APIs. (There is overlap in what you would learn, so the distinction is not cut-and-dried. Please allow me this bit of exaggeration to make a point.)

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
JaMiT
  • 14,422
  • 4
  • 15
  • 31
0

Think of C++ as an extension of C to facilitate object-orientated programming.

C++ contains higher-level features (virtual functions, shared_ptrs, dynamic casts etc) designed to make programming easier, but with the tradeoff of some performance overhead.

These C++ features are built on top of C fundamentals (pointers, malloc etc). So you could avoid virtual functions by using C pointers to functions, but you increase the chance of bugs and readability and maintainability decrease.

So, your C code shouldn't be slower than C++ (unless you do something silly). However, your C++ could be slower until you learn how the C++ features are implemented "under the hood".

user997112
  • 29,025
  • 43
  • 182
  • 361