Besides instructing readers to code that might cause errors if you change this variable (you can use comments for this)
Can not"; will lead to errors in your program.
- The C ++ compiler will use it with compilation errors and diagnostic messages ("compiler errors"), without comment;
- The AC compiler will use it for the most part, although its standard library has openings thanks to a legacy such as
strchr , and has some pretty mild implicit conversion rules that can allow you to discard const without implementing it quite easily. However, just because you received a successful compilation does not mean that you have no errors; Unfortunately, this means that errors can be small errors in your program, as well as large impressive crashes.
In any case, your program is guaranteed to contain an error inside it.
It seems to me that if you do not want the variable to be changed, just do not do it.
It’s good that everything is good and good, but no one is perfect. Programmers are wrong. This allows the compiler - which never makes mistakes (at least not normally); point them to you.
This is especially useful if you are using some kind of data variable, many, many lines of code are where they were created. The further you go, the easier it is to change it without realizing what you should not. For large, complex codebases, this is simply a must.
In your code base you get a new measure of provability , correctness and stability , as well as a huge fragment of the possible causes of really subtle and unpleasant errors. For your compiler (in some cases) there are ample opportunities for optimization when he knows that after compilation some value will not change.
We could list the benefits all day long, but really, you won’t fully test them until you work on such a codebase.
In fact, in an ideal world, all variables will be const by default , and you will need to declare them with the mutable keyword in order to be able to change them. C ++ - back.
Lightness Races in Orbit Aug 10 '13 at 0:41 2013-08-10 00:41
source share