S
Sylvester Hesp
Grizlyk said:Exactly. There are tons of bugs, sucsessully detected by const. Most
system even have hardware const support, because try to do const violation
is just error. Const makes parts of programs more isolated so it is easyer
to maintain. Anyway no one man wants to write into M_PI due to save
memory.
I think no one here is arguing const is useless - it's all about sementics.
Declaring every local variable you never change as const, however, can be a
bridge too far. Just declaring a local var as const because it just so
happens you never change it nearly has no semantic value whatsoever. Sure
M_PI is better as const, it's a constant - you want to keep anyone from
changing it. But local short-lived temporaries which mere use is to cache a
specific calculation or complex function call? That's just a matter of
personal taste, really. For me, const in such situations don't have any
semantic meaning, I'm the only user of the variable and I'm well aware of
what I'm doing with it. It just seems arbitrary to me ("Hey, whatta ya know,
after rearranging and rewriting these statements it turns out I don't change
that temp var anymore. Let's make it const!").
As said, this is my personal opinion, and of course you are entitled to have
your own
situation I described above. What started this whole discussion was the
remark that you _should_ use const whenever you're not going to change a
variable. Const, from a pure C++ perspective, is not about not _going_ to
change, but about not _wanting_ to be able to change. That's a subtle
difference, and that including this being a C++ newsgroup caused my original
remark that it doesn't matter whether a function declaration declares it's
parameters as const values.
For C++ const has a special meaning, const variables can be conpile time
constants if its adress never taken.
Actually, only integral types can be compile time constants, and they are
compile-time constants no matter whether their address has been taken.
- Sylvester