Head Const

Ola Fosheim Grøstad via Digitalmars-d digitalmars-d at puremagic.com
Tue Feb 16 00:29:28 PST 2016


On Tuesday, 16 February 2016 at 08:04:32 UTC, Jonathan M Davis 
wrote:
> For that to work in D, we'd lose out on the guarantee that a 
> const object can't be mutated via a const reference to that 
> data - though with the current state of things, const does 
> become limiting enough that you can't use it under a number of 
> circumstances that you might like to (e.g. ref-counting), and 
> some folks mistakenly think that it's defined behavior to cast 
> away const and mutate and do it in their code.

Yes,  casting away transitive const should be impossible.

The real issue is how to deal with calling overloaded C++ 
functions. Like, some C++ functions implements transitive const, 
others do head const combined with mutable. So it is insufficient 
to look at the C++ sigature... Which means D ought to have some 
stronger way to specifing constness for C++ signatures than C++ 
provides.

> But even if we went that route, that's still a whole other 
> ballgame from non-transitive const. There are some major 
> implications for const if we making it legal to cast it away 
> and mutate as long as the underlying data is mutable rather 
> than immutable, but from a language complexity standpoint, it's 
> not particularly problematic, whereas non-transitive const is.

Yes, please don't allow casting from const to mutable, D should 
aim for stronger typing and better optimization.




More information about the Digitalmars-d mailing list