On Thursday, 24 October 2019 at 14:08:36 UTC, 9898287 wrote:
Does this contain any undefined behavior? It is in C as far as I knew.

immutable int number = 1;
auto bad_idea = (cast(ubyte*) &number)[0 .. number.sizeof];
bad_idea[0] = 2;
writeln(number); //1
writeln(*(cast(int*)bad_idea.ptr)); //2

Cast is a powerful tool and with it you can destroy all type systems guarantees. In your case you will not have any problems just stay away from const and immutable

Reply via email to