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!

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
    – gnat
    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.

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
    – jk.
    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/…
    – david.pfx
    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
    – jk.
    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.
    – david.pfx
    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.
    – user22815
    Oct 9, 2014 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.