Similar to this question, what are the pro/cons to using const local variables like in this answer?
Asked
Active
Viewed 5,201 times
3
-
There seems to be considerable discussion of the pros/cons of the practice at both of the links you provided. Can you elaborate on what points you felt were not covered there and that you think warrant further discussion? – acm Jan 05 '12 at 01:55
-
That question was regarding using `const` function parameters, mine is regarding using `const` local variables. – Jan 05 '12 at 01:58
-
2@Jay: Function parameters *are* local variables. – Benjamin Lindley Jan 05 '12 at 02:03
-
@BenjaminLindley Yes, but that doesn't mean the converse is true. – Jan 05 '12 at 02:04
-
1As it stands, there is no content in this question. Please could you write some words. – Lightness Races in Orbit Jan 05 '12 at 02:08
-
1@Jay : The converse doesn't _need_ to be true, that's completely irrelevant. – ildjarn Jan 05 '12 at 02:13
-
1@ildjarn I disagree. If the other question was about local variables, and my question was about function parameters, then my question would be pointless. But it is the other way around, so my question is different. – Jan 05 '12 at 02:16
-
@BenjaminLindley Not necessary- if the passed in parameters are pointers or references to variables in main - they aren't entirely local since they can be accessed throughout main in addition to the function. I think the OP is talking about variables specifically declared in a function rather than out of it. – fdh Jan 05 '12 at 02:21
-
1@Farhad: References and pointers are whole different animals, we're talking about passing by value. But still, what I said is true of them also, the parameter is not the thing pointed to or referred to, but the pointer or reference itself, which is in fact local. – Benjamin Lindley Jan 05 '12 at 02:28
-
@Jay : Your question is (slightly) different, but the _answer_ is the same. That's what everyone is trying to tell you and you seem to be failing to grasp. ;-] – ildjarn Jan 05 '12 at 02:45
-
@ildjarn They are very similar, but there is still a distinct difference. For example, some of the other question's answers - including the accepted one - where biased by the idea of /always/ making function parameters `const`. – Jan 05 '12 at 02:59
-
@Jay : Yes, for those function parameters that don't change -- and I would argue the same for local variables. Same answer. – ildjarn Jan 05 '12 at 03:11
-
@ildjarn No, they were talking about /all/ function parameters. i.e. having to create another local copy if they wanted to change it. – Jan 05 '12 at 03:17
-
@Jay : Then those answers were wrong and non-idiomatic C++ for anyone who cares about performance. I can't comment much to that, sorry. – ildjarn Jan 05 '12 at 03:31
1 Answers
12
Personally, I like to use const
for any declared object (I'm not sure the word "variable" applies) unless I'm actually going to modify it. It documents to the reader that the value of the object is always going to be whatever it was initialized to, which can make the code easier to follow and analyze.
(It can also help the compiler in some cases, but most compilers, when invoked in optimizing mode, are smart enough to notice that the object is never modified. And if you invoke the compiler in non-optimizing mode, you're telling it that you don't care much about performance.)
In fact, if I were going to design my own language, all declared objects would be const
(read-only) by default unless you explicitly mark them as modifiable.

Keith Thompson
- 254,901
- 44
- 429
- 631