Consequences of casting away immutable from pointers
jmh530
john.michael.hall at gmail.com
Fri Jan 5 13:21:12 UTC 2018
On Friday, 5 January 2018 at 04:10:54 UTC, Steven Schveighoffer
wrote:
>
> Yes, this is undefined behavior.
>
> https://dlang.org/spec/const3.html#removing_with_cast
>
> The compiler assumes x is going to be 5 forever, so instead of
> loading the value at that address, it just loads 5 into a
> register (or maybe it just folds x == 5 into true).
>
> The compiler would likely be free to assume *p_x == 5 forever
> also, if it was clever enough.
>
> I'd recommend not doing this.
>
> -Steve
I should have seen that. Thanks. That makes perfect sense.
More information about the Digitalmars-d-learn
mailing list