Getting the parameters of a struct/class constructor
Andrej Mitrovic
andrej.mitrovich at gmail.com
Thu Jan 24 09:22:16 PST 2013
On 1/24/13, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> On 1/24/13, Joseph Rushton Wakeling <joseph.wakeling at webdrake.net> wrote:
>> ParameterTypeTuple!(A.this)
>
> Use ParameterTypeTuple!(A.__ctor)
>
If you have multiple constructors you can pick the parameters with a
helper template:
import std.traits, std.string;
struct A
{
this(int a, double b)
{
}
this(float y)
{
}
}
template PickCtorParams(Type, size_t index)
{
enum ctorLen = __traits(getOverloads, Type, "__ctor").length;
static if (index < ctorLen)
{
alias ParameterTypeTuple!(__traits(getOverloads, A,
"__ctor")[index]) PickCtorParams;
}
else
{
static assert(0,
format("index %s exceeds %s ctors for type %s", index,
ctorLen, Type.stringof));
}
}
void main()
{
pragma(msg, PickCtorParams!(A, 0)); // (int, double)
pragma(msg, PickCtorParams!(A, 1)); // (float)
pragma(msg, PickCtorParams!(A, 2)); // out of bounds
}
More information about the Digitalmars-d-learn
mailing list