jmegaffin
jmegaffin

Reputation: 1172

"Distributive" variadic templates

I have a variadic template class. Its constructor should accept an instance of a specific class templated on each parameter, i.e.

class Foo<A>
{
public:
    Foo(Bar<A>);
};

class Foo<A, B>
{
public:
    Foo(Bar<A>, Bar<B>);
};

class Foo<A, B, C>
{
public:
    Foo(Bar<A>, Bar<B>, Bar<C>);
};

How can I program this?

Upvotes: 0

Views: 80

Answers (1)

Luc Danton
Luc Danton

Reputation: 35469

template<typename... T>
struct Foo {
    Foo(Bar<T>... bar);
};

In the constructor declaration, Bar<T> as a whole is the pattern that gets expanded such that e.g. for Foo<int, long, double> a constructor taking Bar<int>, Bar<long>, Bar<double> is declared.

Upvotes: 2

Related Questions