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*); withno real time to analyse the problem in depth, the C committee banned any
additional
constother than top level const. (I.e.&p1can beassigned to a
char **or achar **const, but not to achar const**nor a
char const* const*.) The C++ committee did the furtheranalysis, realized that the problem was only present when a
constlevel was followed by a non-
constlevel, and worked out the necessarywording. (See §4.4/4 in the standard.)