Say we take any C library such as libpng or libvorbis and convert it so that it compiles as C++ (only the minimum changes to make it compile as C++ code).

Can the compiler do extra optimizations when its C++ code? Is there any sort of benefit to be gained by porting the code to C++? What are the downsides that might cancel out these benefits?

  • 1
    Unclear what help you need. Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it’s hard to tell what problem you are trying to solve or what aspect of your approach needs to be corrected or explained. See the How to Ask page for help clarifying this question. – gnat May 30 '14 at 14:48
  • When only making the minimum changes to make it compile you probably won't see much/any difference at all. The advantages are in different areas and a program ported in such a way certainly wouldn't be what people call (good) C++ code nowadays anyways. – user1942027 May 30 '14 at 14:50
  • 1
    I made the question clearer (and in line with the answers) by asking about the downsides, as well as benefits of "porting" the application. – Tom Au May 30 '14 at 19:19

Not really. There's no purpose in moving to C++ if you don't make use of the C++ language and library features. It would be effort for nothing, and there are some costs, like ABI.

OTOH, if you do make use of the language features (especially RAII, EH and templates), you will get a far superior program. Many problems can be far easier expressed in C++ than C, and many of them more efficiently too.

Ultimately, moving from C to C++ is an investment. You pay in dev time, probably quite a bit since they're nowhere near as similar as they seem on the surface. You get out vastly improved program. But you have to pay all the investment or it doesn't pay off. It would be like wanting to build a factory but asking for a return after you finished only two walls. You have to build all the walls, build a roof, buy the machinery, hire people to run the factory, find a design to manufacture... then you can start making a profit.

| improve this answer | |

No, and that assuming the code will compile (you will have to make changes, C++ is not completely compatible with C, for example void * values cannot be assigned to other pointer types, and there are new keywords). In best case, there will be absolutely no difference. C++ doesn't add any new undefined behaviors the compilers can depend on. I don't think it removes them either, but I may be wrong here.

In worst case, you will lose the performance. The code that depends on C99's restrict keyword cannot depend on it anymore, as C++ doesn't have restrict keyword.

Also, the compiler has to enable exceptions support, even if it's unused. While I don't think it's needed for the code (it doesn't use C++ types or C++ functions), some compilers still compile the code to handle stack unwinding, which may lower the performance.

Many compilers implement both C and C++ (and probably other languages as well). The C constructs are compiled identically to C++ constructs, as there is no reason for them to behave differently.

| improve this answer | |
  • 3
    All main C++ compilers can disable EH handling, and modern EH is zero-cost. – DeadMG May 30 '14 at 19:28

Is there any sort of benefit to be gained by porting the code to C++?

I think the key phrase here is "porting". What do you mean by that? If you truly port the code to C++, make use of C++' features like templates etc., there is a chance this might speed up the code. (For example, C++' std::sort() is known to be faster than C's qsort() because a C++ compiler can inline the calls to the comparison function.) OTOH, the algorithms used are likely to be tailored to C-style programming, so it might well be that the only way to get a measurable speed gain would be to start from scratch and implement and polish for years to make the library shine in C++. However, a C++ compiler can do more compile-time checks for code written in C++ style, so you would gain there.

But if you only make the C code compile with a C++ compiler, the task is questionable. Given C code, a C++ compiler cannot make (m)any assumptions a C compiler cannot make to base its optimizations on. And C-style code is prone to shutting up the compiler (hard casts, no templates, etc.), so you are unlikely to gain any improved type safety at compile time.

What are the downsides that might cancel out these benefits?

Either way, you need to change the code. That will introduce bugs and incompatibilities. That alone is a big downside that will never be outweighed by the meager chances for improvements listed above.

| improve this answer | |

No, and there is one disadvantage to consider: name mangling. C has very well-defined linking semantics. C++ is by design compiler-specific: this means that converting to C++ may actually hurt you:

  • You can link with only one compiler/linker.
  • The code will not take advantage of any C++ features given the constraints of your question.
  • Introduced risk of new bugs because you are changing the compiler and language.
  • C++ is linker-compatible with C in most cases anyway, so a C++ program can most likely link to a C library without recompiling the library.
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.