How to return a const handle (view) to a mutable member of an agregate

Chris Wright via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 13 15:34:54 PDT 2016


On Sun, 13 Mar 2016 21:14:59 +0000, anonymous wrote:

> On Sunday, 13 March 2016 at 20:10:57 UTC, Basile B. wrote:
>> ref const(Array!Type) view(){}
>>
>> Unless the result is explicitly cast later it can't me modified.
> 
> No, it can't be modified, period. Casting away const and then mutating
> is not allowed, it has undefined behavior.

In theory, it can't be modified. As a practical matter, unions and casts 
will allow people to modify it.

Decorating your code with @safe is intended to prevent these holes (and 
other types of unsafe code). In practice, @safe has some implementation 
flaws, but it will catch the straightforward cases -- and even some of 
the sneaky ones.


More information about the Digitalmars-d-learn mailing list