"Value class instance" pattern?
Benjamin Thaut
code at benjamin-thaut.de
Sat Jul 13 09:27:43 PDT 2013
I also wanted to mention the "ListAvailableCtors" template which is a
nice addition in case there is no constructor available to be called
with the given arguments. It will generate a list of all aviable ctors
with the types of its arguments, and thus greatly improve the error
message given when no appropriate constructor can be found:
string ListAvailableCtors(T)()
{
string result = "";
foreach(t; __traits(getOverloads, T, "__ctor"))
result ~= typeof(t).stringof ~ "\n";
return result;
}
In my original code it was used during construction like this:
static if(is(typeof(result.__ctor(args))))
{
result.__ctor(args);
}
else
{
static assert(args.length == 0 && !is(typeof(T.__ctor)), "Don't know
how to initialize an object of type " ~ T.stringof ~ " with
arguments:\n" ~ ARGS.stringof ~ "\nAvailable ctors:\n" ~
ListAvailableCtors!T() );
}
More information about the Digitalmars-d
mailing list