I understand what const correctness means and my question is not about what const correctness is. So I am not expecting an explanation or C++-FAQ links for that.
My questions are:
- What are the semantic differences between
constin C andconstin C++? and - What is the reason for the difference?
Quotes from the respective standards which make the differences clear would be nice to have.
I regularly switch between C and C++ and I would like to know the important points that one should keep in mind while doing so.
I don't seem to remember the reason for these (special thanks if you can provide a reasoning) but from the top of my mind, I can remember:
- const variables in C++ have internal linkage by default, while in C they have default external linkage;
- const objects can be used as compile-time values in C++, but cannot be used as compile-time values in C;
- Pointers to string literals must be an
char const*in C++ but in C it can bechar*.
What am I missing?
In addition to the differences you cite, and the library differences that Steve Jessop mentions,
is legal in C++, but not in C. Historically, this is because C originally allowed:
Shortly before the standard was adopted, someone realized that this punched a hole in const safety (since
*p2can now be assigned achar const*, which results inp1being assigned achar const*); with no real time to analyse the problem in depth, the C committee banned any additionalconstother than top level const. (I.e.&p1can be assigned to achar **or achar **const, but not to achar const**nor achar const* const*.) The C++ committee did the further analysis, realized that the problem was only present when aconstlevel was followed by a non-constlevel, and worked out the necessary wording. (See §4.4/4 in the standard.)