Casting rules

ag0aep6g anonymous at example.com
Fri Aug 26 21:18:15 UTC 2022


On Friday, 26 August 2022 at 20:42:07 UTC, JG wrote:
> Where can I find rules about casting. e.g. I assume casting 
> away immutable is undefined behavior (or implementation defined 
> behavior). What about casting to immutable (I would expect at 
> most it only to be allowed if your type has no references e.g. 
> ints okay but int[] not etc.) Casting const away (probably not 
> allowed)? Casting to const (probably allowed).
>
> Anyhow guessing aside where can I find the rules?

Casting immutable/const away: 
https://dlang.org/spec/const3.html#removing_with_cast

The cast itself allowed. Mutating the data is not.

Casting to immutable: 
https://dlang.org/spec/const3.html#creating_immutable_data

The cast is allowed as long as you don't mutate the data 
afterwards.

Conversion to const doesn't need a cast. Mutable and immutable 
both implicitly convert to const.


More information about the Digitalmars-d-learn mailing list