Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Const correctness in C vs C++

Tags:

c++

c

constants

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 const in C and const in 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 be char*.

What am I missing?

like image 556
Alok Save Avatar asked Jan 18 '12 09:01

Alok Save


People also ask

Does C have const correctness?

In C, C++, and D, all data types, including those defined by the user, can be declared const , and const-correctness dictates that all variables or objects should be declared as such unless they need to be modified.

Is const better than #define in C?

In general, const is a better option if we have a choice and it can successfully apply to the code. There are situations when #define cannot be replaced by const. For example, #define can take parameters (See this for example). #define can also be used to replace some text in a program with another text.

Is const faster in C?

No, const does not help the compiler make faster code.

Why is const correctness important?

The benefit of const correctness is that it prevents you from inadvertently modifying something you didn't expect would be modified.


1 Answers

In addition to the differences you cite, and the library differences that Steve Jessop mentions,

char* p1; char const* const* p2 = &p1; 

is legal in C++, but not in C. Historically, this is because C originally allowed:

char* p1; char const** p2 = &p1; 

Shortly before the standard was adopted, someone realized that this punched a hole in const safety (since *p2 can now be assigned a char const*, which results in p1 being assigned a char const*); with no real time to analyse the problem in depth, the C committee banned any additional const other than top level const. (I.e. &p1 can be assigned to a char ** or a char **const, but not to a char const** nor a char const* const*.) The C++ committee did the further analysis, realized that the problem was only present when a const level was followed by a non-const level, and worked out the necessary wording. (See §4.4/4 in the standard.)

like image 162
James Kanze Avatar answered Sep 20 '22 15:09

James Kanze