3 ms·
Do you find const actually catches bugs? I admittedly don't use C++ that frequently, but if I make my code const-correct it always seems to cause a lot of fals
by alextgordon 16y ago
Do you find const actually catches bugs?
I admittedly don't use C++ that frequently, but if I make my code const-correct it always seems to cause a lot of false positives, while seldom finding actual bugs.
- danieldk 16y agoI disagree, especially const-correct methods help avoiding mutation. The primary mistake IMO is that const can be cast away. Invariant should be invariant (like D).
- jpr 16y agoA lot of people seem to be horrified that you can cast constness away. Maybe they have more traumatic experiences than I do, but I believe that if something is possible to do (modifiying an object that is declared const), it should be possible in the language as well. That does not mean it should have well specified semantics or that it should be done without proper considerations.
- nickelplate 16y agoconst does not so much catch bugs as it prevents them. It is a way to specify a contract. If your code is const-correct, the next programmer using or maintaining your code can read a function or method signature and know immediately which parameters will not change, or whether the object will be modified. Effective C++ explains this very well, and I fell in love with const after reading its second edition almost 10 years ago.