Extracting template parameters
Simen Kjaeraas
simen.kjaras at gmail.com
Tue Nov 6 08:01:44 PST 2012
On 2012-11-06, 16:20, Joseph Rushton Wakeling wrote:
> Suppose that I have two struct templates which take identical parameter
> lists:
>
> struct Foo(T1, T2, T3)
> {
> ...
> }
>
> struct Bar(T1, T2, T3)
> {
> ...
> }
>
> Now suppose that I have a Foo which has been instantiated with a given
> set of parameters. Is there any way for me to say, "now instantiate a
> Bar with the same parameters?"
>
> The use-case I'm thinking of is a function something like this (somewhat
> pseudo-code-y):
>
> auto fooToBar(FooInstance f)
> {
> Bar!(f.T1, f.T2, f.T3) b;
> // set values etc.
> return b;
> }
>
> Of course the f.T1 notation is my fiction, but it gives the idea of what
> is needed -- is there a means to extract and use template parameters in
> this way? I assume something from std.traits but it's not entirely clear
> what or how ...
In addition to Dan's answer, let me present a general solution:
template InstantiationInfo( T ) {
static if ( is( T t == U!V, alias U, V... ) ) {
alias U Template;
alias V Parameters;
} else {
static assert(false, T.stringof ~ " is not a template type
instantiation.");
}
}
With this, you can extract the parameters to a template
(InstantiationInfo!Foo.Parameters) or the template used
(InstantiationInfo!Foo.Template).
--
Simen
More information about the Digitalmars-d-learn
mailing list