Workarounds for Lack of Mutable Keyword
Craig Black
craigblack2 at cox.net
Thu Apr 3 01:21:22 PDT 2008
"Janice Caron" <caron800 at googlemail.com> wrote in message
news:mailman.301.1207206839.2351.digitalmars-d at puremagic.com...
> On 03/04/2008, Craig Black <craigblack2 at cox.net> wrote:
>> void ConstAssign(T)(ref const T x, T y) { *cast(T*)&x = y; }
>
> One problem with this function is demonstrated by
>
> string s = "hello world";
>
> ConstAssign(h[0],'j');
>
> Oops - it assigns invariants too! Even if you can be sure there are no
> other threads vying for access to s, still, those chars might be in a
> hardware-locked ROM segment.
>
> There's a reason why casting away const is not defined.
If that compiles, I think it may be a bug. Invariant types shouldn't be
implicitly convertible to const.
-Craig
More information about the Digitalmars-d
mailing list