www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Casting rules

reply JG <someone somewhere.com> writes:
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?
Aug 26 2022
parent reply ag0aep6g <anonymous example.com> writes:
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.
Aug 26 2022
parent JG <someone simewhere.com> writes:
On Friday, 26 August 2022 at 21:18:15 UTC, ag0aep6g wrote:
 On Friday, 26 August 2022 at 20:42:07 UTC, JG wrote:
 [...]
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.
Thank you very much.
Aug 27 2022