Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

C++ is not backwards compatible with C. It’s mostly compatible, but it’s not completely compatible like say Objective C which is a strict superset of C.


They have diverged slightly in the years since C++ was introduced. But at the time the incompatibilities were extremely small. Even today they are pretty small and GCC/Clang will happily compile C constructs (e.g. designated initialisers, VLAs) with just a warning.

Actually you need `-Wpedantic` to even get a warning for both of those. (And `-std=c++17` since designated initialisers are actually in C++20.)


...at least as long as you don't go anywhere near the atomic mess.


> C++ is not backwards compatible with C.

Yeah, it is really ironic. C++ got all the warts of C to be compatible with it and then C++ had lost its compatibility with C but not warts.


honestly, no one cares about splitting that hair. Most valid C programs will compile in C++ and believe it or not, binary logic is not the only logic humans are capable of.


Almost every C program does `int *c = malloc(sizeof(int) *10)` or similar, which has always been invalid C++.


I believe most compilers have switches to allow this. The real issue you might run into that can't be worked around easily is if the C source uses a C++ keyword as an identifier.


    #define new new_


actually, i reckon that most, if any, c programs will not compile with c++, without at least some (possibly not difficult) modifications


My only qualms is the number of genuine C only compilers in use, vs number of embedded system companies/projects/codebases in the world.

The subset of C features that are incompatible with C++ is quite small, and most of the features are a little out of the way IMO.

Of the regular suspects (GCC, MSVC, Clang), your C codebase is probably entirely compatible with C++ as each of these has varying levels of compliance with the C and C++ standard.


> Of the regular suspects (GCC, MSVC, Clang), your C codebase is probably entirely compatible with C++ as each of these has varying levels of compliance with the C and C++ standard.

yep, leave it up to the technical crowd to try and split hairs too fine.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: