skypjack
skypjack

Reputation: 50550

Template argument deduction for class templates and multiple parameters packs

In C++17 template arguments for a class template will be deduced more or less as it happens nowadays for a function template.
Here is the relevant paper.

As an example from the above mentioned paper:

template<class ... Ts> struct X { X(Ts...) };
X x1{1}; // OK X<int>
X x11; // OK X<>

Function templates have another interesting feature when deduction happens.
Consider the following code:

template<typename... U, typename... T>
auto func(T&&...) {}

// ...

// U is int, char - T is int, double
func<int, char>(0, .0);

We can have two parameters packs as long as deduction helps to discriminate between them.
No need to wrap them within a tuple or some other structure.

Will it be possible to do the same with class templates?

As an example:

template<typename... U, typename... T>
struct S {
    S(T&&...) {}
};

// ...

// U is int, char - T is int, double
S<int, char> s{0, .0};

The paper contains the example below:

template<class ... Ts> struct X { X(Ts...) };
X<int> x3{1, 'a', "bc"}; // OK X<int,char,const char*>

Anyway, it isn't exactly the same thing and I'm not sure if it will be allowed or not.

Upvotes: 3

Views: 258

Answers (1)

Barry
Barry

Reputation: 303107

This:

template<typename... U, typename... T>
struct S { ... };

is just ill-formed per [temp.param]:

If a template-parameter of a primary class template, primary variable template, or alias template is a template parameter pack, it shall be the last template-parameter.

This case:

template<class ... Ts> struct X { X(Ts...) };
X<int> x3{1, 'a', "bc"}; // OK X<int,char,const char*>

is problematic since X<int> is already a valid type. This part of the paper was dropped in Oulu, though it's possible some proposal in the future will make it possible to indicate that some of the class template parameters should be deduced but others should be specified:

X<string, ???> x3{"a", 1, "b"}; // X<string, int, const char*>.

where ??? is some series of tokens that makes intent clear.

Upvotes: 4

Related Questions