5th Dec 2009
A Question On Indirect Constness
You may have used code such as [1]:
Here we obtain a pointer b to the same object that a points to, but we say that it is now immutable (through b, at least).
However, C++ does not allow a programmer to add constness more than one layer of indirection away [2]:
Of course, you can still make the pointer itself const [3]:
So it turns out that you can add constness to the pointer [3], or to the immediate pointee [1], but you can't add constness to the pointee of the pointee [2].
The question is: why not? How does adding constness violate const-correctness? Answers on a postcard, please.
Update: As Kniht correctly pointed out in the comments (and, in fact, as the standard itself explains in a note), this is invalid for very good reason:








http://bitbucket.org/kniht/scraps/src/tip/cpp/examples/subvert-const.c
Aha! That explains that, thanks! Turns out that this explanation is written out in the standard, too. Silly me.