Persistent list

Ola Fosheim Grøstad via Digitalmars-d digitalmars-d at puremagic.com
Sun Nov 15 10:26:55 PST 2015


On Sunday, 15 November 2015 at 15:14:38 UTC, Jonathan M Davis 
wrote:
> mutating is undefined behavior, arguing that if it weren't, it 
> would be like C++'s const and that C++'s const is pretty much 
> useless, because it doesn't provide actual guarantees.

Hm, I think C++ const requires that the object isn't modified, 
but you can cast away const in order to call functions that are 
erroneously missing a const specifier.

C++'s solution is to have a "mutable" specifier for fields like 
that can be mutated without affecting the type's state externally 
(mutexes etc).

 From the C++ faq:

«NOTE: there is an extremely unlikely error that can occur with 
const_cast. It only happens when three very rare things are 
combined at the same time: a data member that ought to be mutable 
(such as is discussed above), a compiler that doesn’t support the 
mutable keyword and/or a programmer who doesn’t use it, and an 
object that was originally defined to be const (as opposed to a 
normal, non-const object that is pointed to by a 
pointer-to-const). Although this combination is so rare that it 
may never happen to you, if it ever did happen, the code may not 
work (the Standard says the behavior is undefined).»

So const_casting away const followed by mutation is undefined 
behaviour.



More information about the Digitalmars-d mailing list