On Tuesday, 22 December 2020 at 17:20:03 UTC, ag0aep6g wrote:
On Tuesday, 22 December 2020 at 16:53:11 UTC, jmh530 wrote:
For
v = cast(size_t) x;
I thought @safe prevented explicitly casting an immutable to a mutable, but the code below seems to suggest it is ok in this case...

void main() @safe
{
    immutable x = 32;
    auto v = cast(size_t) x;
}

It's ok because you're making a copy. Casting from immutable to mutable is only dangerous when altering the mutable thing would affect the immutable thing, as it happens with pointers and such.

Ah, I get the error when I keep v as a pointer (or replace x with a cast). I had tried below and didn't have errors, but if you change the cast to cast(size_t*) then you get the error. Thanks for that.

void main() @safe
{
    immutable int* x = new int(32);
    auto v = cast(size_t) x;
    v++;
}

Reply via email to