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

Basile B. via Digitalmars-d digitalmars-d at puremagic.com
Fri Jan 1 11:15:56 PST 2016


On Friday, 1 January 2016 at 16:13:47 UTC, Adam D. Ruppe wrote:
> On Friday, 1 January 2016 at 15:55:53 UTC, Bottled Gin wrote:
>> That gives me a false positive if a defined constructor has
>
> The helper loops through all the overloads looking for one that 
> is assignable to a zero-arg pointer - meaning it matches the 
> default signature, ignoring default params.
>
> Then it just returns if it found one.

Why not just checking that a ctor has no parameter ?

~~~~~~
template HasDefaultConstructor(Class)
{
     bool helper()
     {
         bool result;
	foreach(overload; __traits(getOverloads, Class, "__ctor"))
         {
             auto fun = &overload;
             import std.traits: Parameters;
	    static if((Parameters!fun).length == 0)
                 result = true;
	}
         return result;
     }
     enum HasDefaultConstructor = helper();
}
~~~~~~


More information about the Digitalmars-d mailing list