On 02/27/2018 09:30 PM, Radu wrote:
enum Type { a };
struct S(Type t = Type.a)
{
this(Type)(Type t)
{
import std.stdio;
writeln("ctor called.");
}
}
void main()
{
auto x = S!(Type.a)(Type.a);
void* y = &x;
auto z = (cast(S!(Type.a)) y);
}
Surprisingly the cast will actually call the ctor. Is this to be
expected? Sure looks like a bug to me, as a non templated S will
complain about the cast.
Not a bug. The spec says [1]: "Casting a value v to a struct S, when
value is not a struct of the same type, is equivalent to: S(v)"
Templates have nothing to do with it. Your code boils down to this:
----
struct S
{
this(void* t)
{
import std.stdio;
writeln("ctor called.");
}
}
void main()
{
void* y;
auto z = cast(S) y;
}
----
[1] https://dlang.org/spec/expression.html#cast_expressions