C ++ 11: Is it possible to set a template with a template parameter with the varidic-template-template-parameter parameter?

(Yes, the name is so strange thanks to my poor English, I hope someone can improve it.)

Answering this question , I found that this code works:

template <typename T1, typename T2> class A { }; template <template <typename...> class U> class B { }; int main() { B<A> it_works; } 

.. although template <typename...> class and template <typename, typename> class not equal.

I tried to find out why this is possible and look at [temp.param] N3337 standard , but I canโ€™t find anything. How is this possible?

+6
source share
2 answers

[temp.arg.template] describes how template template arguments are mapped to the corresponding parameter. In particular, C ++ 11 14.3.3 / 3 says:

When the template parameter list Ps contains a template parameter package, the template parameter package will correspond to zero or more template parameters or template parameter packages in the template parameter list A with the same type and shape as the template parameter package in P

where P is the parameter ( U in your example) and A is the argument ( A in your example). Thus, your example with a parameter package will match a template with any number of type parameters.

+3
source

Yes it is possible. C ++ 11 14.3.3 / 3 specifically allows this and provides an example.

3 The template argument corresponds to the template template (name it P ), when each of the template parameters in the template parameter list of argument templates, the corresponding template template or alias template (call it A ) corresponds to the corresponding template parameter in the list of template parameters P When P s template-parameter-list contains a template parameter package (14.5.3), the template parameter package will match zero or more template parameters or template parameter packages in the template parameter list A with the same type and shape as the template parameter package in P (ignoring whether these template parameters are template parameter packages) [Example:

 template <class T> struct eval; template <template <class, class...> class TT, class T1, class... Rest> struct eval<TT<T1, Rest...>> { }; template <class T1> struct A; template <class T1, class T2> struct B; template <int N> struct C; template <class T1, int N> struct D; template <class T1, class T2, int N = 17> struct E; eval<A<int>> eA; // OK: matches partial specialization of eval eval<B<int, float>> eB; // OK: matches partial specialization of eval eval<C<17>> eC; // error: C does not match TT in partial specialization eval<D<int, 17>> eD; // error: D does not match TT in partial specialization eval<E<int, float>> eE; // error: E does not match TT in partial specialization 

-end example]

(Emphasis mine)

+5
source

Source: https://habr.com/ru/post/975885/


All Articles