Workarounds for Lack of Mutable Keyword

Janice Caron caron800 at googlemail.com
Thu Apr 3 00:06:52 PDT 2008


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.



More information about the Digitalmars-d mailing list