Logical const

Jesse Phillips jessekphillips+D at gmail.com
Mon Nov 29 16:54:18 PST 2010


Simen kjaeraas Wrote:

> Steven Schveighoffer <schveiguy at yahoo.com> wrote:
> 
> > Except the language says what results from your code (casting away const  
> > and then mutating) is undefined behavior.  This means all bets are off,  
> > it can crash your program.  I'm unsure how the compiler could take that  
> > route, but that's what's in the spec.
> >
> > Maybe because of the way const works, it never really is undefined, but  
> > there will always be that loophole.
> 
> The thing is, immutable is implicitly castable to const, and immutable
> data could be stored in write-protected memory. Apart from that, I believe
> it is safe to cast away const.
> 
> -- 
> Simen

In fact, couldn't opAssign be


     ref opAssign(U)(auto ref U v)  if(Unqual!U == U)
     {
         return mutable() = v;
     }

This way only mutable references are only ever assigned to it? Then it can be marked as @trusted and used in safe code as it will never cause the program to crash?


More information about the Digitalmars-d mailing list