Casting between Structs
jxel
jxel at gmall.com
Sat Feb 8 19:28:26 UTC 2020
On Saturday, 8 February 2020 at 17:36:17 UTC, Jonathan Marler
wrote:
> I reduced an issue in my project to this code snippet:
>
> struct Struct1 { }
> struct Struct2 { }
>
> void func1(const Struct1);
> void func2()
> {
> Struct2 s;
> func1(cast(Struct1)s);
> }
>
>
> DMD compiles this just fine but according to the spec this
> should not compile. The spec
> (https://dlang.org/spec/expression.html#CastExpression section
> 8) says that "Casting a value v to a struct S, when value is
> not a struct of the same type, is equivalent to: S(v)",
> however, if I write Struct1(s) in this example, I get a compile
> error.
>
> Is the spec wrong or is this a problem in the DMD compiler?
>
> P.S. this snippet causes LDC to segfault
> (https://github.com/ldc-developers/ldc/issues/3314), but it's
> not clear whether it should be made to work like DMD, or if
> they should conform to the spec and make this a compile error
The spec is wrong in many aspects. It was written then never
followed. Not sure if it even gets updated.
This seems like it should be an error. I don't know why you would
ever want to do a cast like that, especially with lvalues. And if
you did want to there is an easy alternative *cast(S1*)&v.
More information about the Digitalmars-d
mailing list