How to check at compile time if Default Constructor for a class exists?

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Jan 1 09:06:38 PST 2016


On Friday, 1 January 2016 at 15:55:53 UTC, Bottled Gin wrote:
>> You might be able to just do `static if(__traits(compiles, new 
>> YourClass()))` and if it returns true then figure it does.
>
> That gives me a false positive if a defined constructor has 
> default value for all its arguments.

And how is that not a default constructor then? What semantic 
difference is there between

this()
{
     this(defValue);
}

this(T t)
{
     ...
}

and

this(T t = defValue)
{
     ...
}

The only way that I can think of that it could even matter would 
be if you were trying to take the address of the constructor, 
which would be a rather odd thing to do.

It seems like saying that a constructor with all default 
arguments is not a default constructor is a distinction without 
meaning. What are you doing that you would care?

- Jonathan M Davis


More information about the Digitalmars-d mailing list