What are the known shortfalls of const
in C++ and C++0x?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
The problem with const is the programmers that use it incorrectly our inconsistently
One thing is that it is still possible subvert it. i.e. it is still legal to do something like this:
You can even use things like
memset
to subvert it without an explicitconst_cast
.This is a trade-off between having the compiler enforce
const
and allowing some flexibility for nonconst
aware interfaces.Which leads to another limitation, in that it has not been universally embraced, which is in part due to another problem, which is that using
const
is an all-or-nothing proposition. If you start using it, you will need to propagate it throughout your code base.const
is great.const
is important.const
-correctness is necessary condition for an API to be good.Yet there are two issue I've had with
const
, repeatedly.There's no way to mark a variable as
const
retroactively. You must either declare a variable code, in which case you have to initialize it immediatly. What if the initialization code contains anif
, though? You have the choice of either omitting theconst
(undesirably), using operator?
instead ofif
(harms readability). Java get's that right, BTW -const
variables don't have to be initialized right away, they just have to be initialized before they're first read, and they have to be initialized in all branches of anif
.There's no way to specifiy that an object passed by reference to a function won't change for the duration of the function call.
const T& t
does not mean that the object pointed to byt
won't change, but only that the referencet
cannot be used to change it. The compiller still has to assume that any function call which it does not see into might change the object. It some cases, that prevents quite a few optimizations.The only thing wrong with
const
is that it is seriously underrated by to many developers. It's one of the best tools in C++'s toolbox, very sharp, and yet not dangerous to cut yourself with.The main problem is that you have to write it. It should be the default, and all mutable variables or parameters should be specified explicitly.
Most of the answers below state things such as "what is wrong with
const
is that X people do Y". Those are not answers but symptoms. Those are not things wrong withconst
. There's barely any wrong thing withconst
... Those are things wrong with people who can't RTFM.