Struct ctor called with cast

Steven Schveighoffer schveiguy at yahoo.com
Tue Feb 27 21:07:52 UTC 2018


On 2/27/18 3:59 PM, Radu wrote:
> On Tuesday, 27 February 2018 at 20:51:25 UTC, ag0aep6g wrote:
>> 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);
>>> }

[snip]

> So the bug is that somehow the templated version makes it so there is an 
> implicit void* ctor.

Look at your constructor. You actually have a TEMPLATED constructor 
inside a TEMPLATED type.

In other words, inside your constructor, `Type` is not an enum Type, 
it's actually a void *.

It becomes clearer if you change the name of the second template parameter:

struct S(Type t = Type.a)
{
    this(T)(T t)
    {
        import std.stdio;
        writeln("ctor called.");
    }
}

-Steve


More information about the Digitalmars-d-learn mailing list