mutable destructor? WAT???
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Aug 29 11:37:41 PDT 2016
On 8/29/16 12:05 PM, Steven Schveighoffer wrote:
> On 8/28/16 6:35 AM, Dicebot wrote:
>> Looks correct to me. This const annotation does not prevent you from
>> deleting memory or free'ing external resources - but it does ensure no
>> transitive mutations for data reachable from struct fields. If it
>> allowed destroying with mutable destructor, type system hole like this
>> would be legal:
>>
>> struct S
>> {
>> char[] str;
>> ~this() { str[0] = 'a'; }
>> }
>>
>> auto s = new const S("abcd");
>> destroy(s); // mutates immutable literal
>
> void foo(const(S) str) {}
>
> void main()
> {
> char[1] str = ['0'];
> auto s = S(str[]);
> foo(s);
> writeln(str[]);
> }
Sorry if not obvious, the writeln prints "a". So the destructor was run
as foo exits.
-Steve
More information about the Digitalmars-d-learn
mailing list