Code behaving differently in C90, C99, C11, C++98, and C++11
kristerw.blogspot.com
kristerw.blogspot.com
This post never mentions C++'s backwards compatibility. It points out these 3 examples:
// comments
C90 does not have // comments, so constructs of the form
...i.e. more recent version of C and C++ is a super set of C90 here, but writing valid C90 (without using // comments), would compile file in C90 and above.
Wide string literals
C11 and C++11 have wide string literals where for example U"hello!"
Same reasoning for these, you would never use U"hello!" in pre C11/C++11.The 3rd example is a fundamental difference between C and C++:
Character constants, such as 'a', have type int in C while C++ use the type char. This means that sizeof('a') evaluates to a different value for C and C++.
This is not about backward compatibility, backward compatibility means : your code written in C++98, still works if compiled in C++11, it does not say : "write code using newer C++98/11/.../ and try to compile it in a previous version (or a different language) and see what happens."To an extreme it's like saying:
template<class A>
struct foo {
// some methods with use of lambda, variadic templates, ...
};
"Hey look, the above example doesn't work with C++98, C++ backward compatibility is a myth!"> backward compatibility means your code written in C++98, still works if compiled in C++11
> writing valid C90 (without using // comments), would compile file in C90 and above.
Aye. There's the rub: backwards compatibility is broken by introducing // as int i = 2 //* */2 which is perfectly valid code in C90 evaluates to 1 while 2 in later version. When releasing later versions you are always breaking someones insane code. It's a matter of compromises between breaking 0.00000000001% of the codebase and progress. The number of zeroes in that percentage is of course debatable, see http://www.cl.cam.ac.uk/~pes20/cerberus/notes50-survey-discu... where a number of "Do you know of real code that relies on it?" are answered by ~20% of yes and ~20% of no, that would be crazy. So it is entirely possible the standards committee believes a construct is not used, that'd be crazy but it actually is used.
http://www.softwarepreservation.org/projects/c_plus_plus/ind... leads me to http://www.softwarepreservation.org/projects/c_plus_plus/cfr..., which shows that the first external C++ release didn't have // comments.
The first commercial release had, though (http://www.softwarepreservation.org/projects/c_plus_plus/cfr...)
I was only thinking of the standardized ones, but you're correct, of course!
I have seen real world code which does almost exactly that and which broke (fortunately with a compiler error rather than silently changed code) when compiled with a C++11 compiler due to the new user-defined literal syntax. I agree that it's rare, but it can happen.
Which means you're never in the position that "this update broke our code", barring of course compiler bugs and switches to a new ABI, both of which are extremely rare.
Updating the code to function under C++11 requires adding a noexcept(false) specification to throwing destructors.
But still, pretty neat in terms of features.