Const initialization issue, looking for solution

Jakob Ovrum jakobovrum at gmail.com
Wed May 29 06:30:08 PDT 2013


On Wednesday, 29 May 2013 at 13:21:21 UTC, Maxim Fomin wrote:
>
> Fundamental issue here is that
>
> const T t;
>
> is almost useless, it is essentially immutable since you cannot 
> change it and you cannot alias it. As such, it probably should 
> be changed to a mutable object or an immutable one.

It's generic code. See the linked code and you'll see that this 
is not an option.

> However, since const/mutable aliasing is allowed, you can do:
>
> union U (T)
> {
>    const T ct;
>    T mt;
> }
>
> because const doesn't mean that object would never change, you 
> can mutate it through alias. From the opposite view, there is 
> also no problem in reinterpeting a mutable object as const 
> object (since mutables can be converted to const).
>
> Depending on your mutable indirection situation this can work 
> or may not.

Mutable indirection is actually not the problem with this 
solution, it's the casting away of const. There's no guarantee 
here that the data wasn't actually immutable when it was created.


More information about the Digitalmars-d mailing list