Casting between structs of the same size is treated as a bit cast?

jfondren julian.fondren at gmail.com
Wed Oct 20 04:26:20 UTC 2021


On Wednesday, 20 October 2021 at 04:14:37 UTC, Dave P. wrote:
> I am confused on how casting structs works. According to point 
> 9 of https://dlang.org/spec/expression.html#CastExpression:
>
>> Casting a value v to a struct S, when value is not a struct of 
>> the same type, is equivalent to:
>>
>> ```d
>> S(v)
>> ```

It says `value v` and `struct S`, but you go on to cast a struct 
into another struct.

Point 9 is exhibited by

```d
struct Foo {
     int i;
}

unittest {
     int n = 2;
     Foo f = cast(Foo) n;
}
```

> However, the following program compiles and the resulting 
> execution indicates the bits are just being reinterpreted.
...
> Is this a bug in my understanding? Bug in the spec? Bug in the 
> compiler?

It looks at least like a gap in the spec. Adding "or a struct to 
another struct" to point six would fit the observed behavior.


More information about the Digitalmars-d-learn mailing list