4

I would like to know what are advantages and disadvantages when using the optional const qualifier when initializing non-ref/pointer variables with a copy of a value:

for example:

  • void f(const T v) instead of void f(T v) // v does not need to be changed
  • if (const int err = f()) {/*...*/} instead of if (int err = f()) {/*...*/}
  • or even void f() {const T* const v = p; /*...*/} instead of void f() {const T* v = p; /*...*/}

Is it just a matter of style? What does the C++11 standard use in its examples? Could not const be an hint for the compiler to store the variables in some special read-only memory (in some implementations)?

Martin
  • 9,089
  • 11
  • 52
  • 87
  • 3
    You should decide between C and C++, `const` means different things for them. – Xeo Mar 15 '13 at 10:28
  • @Xeo I am talking about C++ – Martin Mar 15 '13 at 10:29
  • Top level `const` qualifiers (and `volatile`) are ignored in declarations of function parameters - `void f(const int)` and `void f(int)` are identical function declarations. But the two declarations in your last bullet are different - in fuction signature `const T* const` does not mean the same as `T* const`. – jrok Mar 15 '13 at 10:33
  • jrok, yes, I have fixed my question, have a look again please – Martin Mar 15 '13 at 10:35
  • @Xeo what is the difference in by-value parameter const-ness in C and C++? – Alex B Mar 15 '13 at 10:36
  • @Martin alright. The first sentence of my previous comment still aplies, though. – jrok Mar 15 '13 at 10:37
  • @AlexB: Mainly the fact that `const` in C only means "read-only", and `const` variables cannot result in a constant expression - for example `const int n = 20; switch(sth){ case n: ... }` is fine in C++, because `n` is constant-initialized and as such yields a constant expression, whereas C doesn't know any of that. – Xeo Mar 15 '13 at 10:47
  • [Here](http://stackoverflow.com/questions/212237/constants-and-compiler-optimization-in-c) is another interesting discussion. – Arun Mar 15 '13 at 11:10
  • @Xeo sure, but I was specifically curious how it's different for function parameters, which is what Martin is asking. – Alex B Mar 16 '13 at 04:15

4 Answers4

2

In such cases const is sort of a reminder to yourself that this variable is not supposed to change. So that later (probably much later), when you need to modify this function, you will not accidentally change the variable and break other code in this function that depends on the variable's immutability. Compiler will only store const variables in read-only memory if the variables type (class) has trivial constructor.

user2116939
  • 454
  • 4
  • 5
  • With C++11 rules, you now also have `constexpr` which makes it easier for a compiler to store nontrivially-initialized `const` objects in ROM. – MSalters Mar 15 '13 at 10:33
1

const in these 3 contexts mean that you can't alter the variable. But if you left it out, the compiler would still see that you don't alter the variable. Modern compilers will check all assignments to a variable, and spot that there's just a single initialization.

MSalters
  • 173,980
  • 10
  • 155
  • 350
1

Nope, it's worthless. There are no real examples of compiler optimizations based on const. There's no benefit to declaring such variables as const.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • It can make it easier to reason about code. E.g. in safety critical code values have to be declared `const` if they are const. – Peter Wood Mar 15 '13 at 10:56
  • Wrong answer. `const` makes code readable for the reader. The reader is sure that this variable is not going to change (unless some dirty casting). – iammilind Mar 15 '13 at 11:29
0

Function prototype like this one:

void f(const T& arg);

says to the caller: "I will not change the passed argument although you pass it by reference".

But for passing by value:

void f(const T arg);

the changes to the passed argument are not visible to the caller because copy is being created, thus for the caller it makes no difference whether it's const or not. But it says: "After the copy of the argument is created, this copy will not change during the execution of this function", which might be useful when you want to make sure that the person who will implement this function will treat it as a constant.

But apart from making the design of your code to be easier to understand by the people who will read it in the future, using const qualifier doesn't make much sense here.

LihO
  • 41,190
  • 11
  • 99
  • 167