5

It's not clear to me what benefits there are of declaring your stack variables as constant in C++, I was hoping somebody might explain the benefits and purpose for this technique.

For example:

void func(const std::string& arg)
{
 if(someCondition)
 {
 const std::string foo ("some string plus " + arg);
 std::out << foo << std::endl;
 someFunction(foo);
 // dozens more lines of code...
 }
 // bla bla bla...
}

Thanks!

Jimmy Hoffa
16.2k3 gold badges71 silver badges80 bronze badges
asked Feb 19, 2014 at 1:23
1
  • 2
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask Commented Feb 19, 2014 at 4:25

1 Answer 1

13

Yes.

  1. It helps the reader to understand your intent. Clearly foo is given an initial value and never changed thereafter.

  2. It helps the compiler to optimise your code, for both speed and space. The compiler can perform certain optimisations such as hoisting values out of loops if there is a promise that the value cannot change. This is particularly the case if the value has an alias, which would otherwise defeat optimisation.

  3. It occasionally catches bugs, when you accidentally write code to modify something you didn't mean to.

On the down side, you may not be able to pass the value into a function that takes non-const arguments.

answered Feb 19, 2014 at 9:14
10
  • 2
    do you have any reference for 2, my understanding was that this isn't true as 1) compilers can do this optimization regardless of if the variable is actually marked const, 2) const only promises a logical constness i.e. there can actually be mutation 3) the ability to cast away const means all bets are off Commented Feb 19, 2014 at 10:02
  • A quick web search found dozens, mostly right here on SO. Try this for a start: stackoverflow.com/questions/212237/… Commented Feb 19, 2014 at 10:08
  • 2
    and I can find many saying it doesn't help stackoverflow.com/questions/6313730/… - I do take the point that it at least allows the compiler to potentially optimize Commented Feb 19, 2014 at 10:14
  • 3
    @jk.: the answer to this question includes an example (3rd code block) that is exactly what I had in mind. Aliasing (pointer to declared variable) prevents the compiler optimising; const allows it to do so. Not much point pursuing this further -- it's been done to death. Suggest you ask the GCC authors. Commented Feb 19, 2014 at 10:28
  • @everyone: Implementation-defined behavior is implementation-defined. Just because one compiler can optimize it does not mean all can. Commented Oct 9, 2014 at 0:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.