Variadic template template arguments

The following code does not compile using clang 3.0, is it because I did it wrong? Because it is not allowed in C ++ 11 or because it is not supported in clang?

template<int OFFSET> struct A { enum O { offset = OFFSET }; }; template < template <int T> class Head, typename... Tail> struct C : public Head<1>, private C<Tail> { }; int main() { C< A, A > c1; return 0; } 

Compiler Error:

 test3.cxx:99:42: error: template argument for template template parameter must be a class template or type alias template struct C : public Head<1>, private C<Tail> { }; ^ test3.cxx:103:15: error: use of class template A requires template arguments C< A, A > c1; ^ test3.cxx:94:12: note: template is declared here struct A { ^ 2 errors generated. 
+4
source share
1 answer

Three questions:

Tail should be a variable list of templates, not types. Therefore, it should be

 template<int> class... Tail 

instead

 typename... Tail 

and you need to explicitly expand the parameter package with private C<Tail...> instead of private C<Tail> .

And you will need to implement the basic case when Tail... empty:

 // base case template < template <int> class Head> struct C<Head> : public Head<1> { }; 

(This is a compilation for Clang 3.0)

Now the whole piece of code:

 template<int OFFSET> struct A { enum O { offset = OFFSET }; }; template < template <int> class Head, template<int> class... Tail> struct C : public Head<1>, private C<Tail...> { }; template < template <int> class Head> struct C<Head> : public Head<1> { }; int main() { C< A, A > c1; return 0; } 
+5
source

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


All Articles