Template type deduction
Philippe Sigaud
philippe.sigaud at gmail.com
Sun Jan 20 08:10:59 PST 2013
On Sun, Jan 20, 2013 at 5:01 PM, Phil Lavoie <maidenphil at hotmail.com> wrote:
> Is there also a way to extract per parameter information (ex: type) using
> type deduction. Let me illustrate:
>
> struct Toto( T1, T2 ) {
> alias Key = T1;
> alias Value = T2;
> }
>
> struct OuterToto( T ) if( isToto!T ) {
> T.Key key;
> T.Value value;
> }
>
> Imagine I would want that but without using inner aliases, is this possible?
Sure, the U... part is accessible inside the static if. Just define a
generic helper template:
struct Toto(First, Second)
{
}
template TemplateArguments(T)
{
static if (is(T _ : a!(Args), alias a, Args...))
alias TemplateArguments = Args;
else
alias TemplateArguments = void;
}
template TemplateName(T)
{
static if (is(T _ : a!(Args), alias a, Args...))
alias TemplateName = a;
else
alias TemplateName = void;
}
void main(string[] args)
{
alias Type = Toto!(int, double);
writeln(TemplateArguments!(Type).stringof);
writeln(TemplateName!(Type).stringof);
alias T = TemplateName!(Type);
alias Swapped = T!(double, int); // And you can use T as a template.
}
As you can see, you can even use the deduced template name.
More information about the Digitalmars-d-learn
mailing list