All*_*lan 4 c++ variadic-templates c++11
以下代码不使用clang 3.0进行编译,这是因为我做错了吗?因为在c ++ 11中不允许它,或者因为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;
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
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.
Run Code Online (Sandbox Code Playgroud)
三个问题:
Tail是一个可变的模板列表,而不是类型.因此它应该是
template<int> class... Tail
Run Code Online (Sandbox Code Playgroud)
代替
typename... Tail
Run Code Online (Sandbox Code Playgroud)
并且您需要使用private C<Tail...>而不是显式扩展参数包private C<Tail>.
你需要实现基本情况,因为什么时候Tail...是空的:
// base case
template < template <int> class Head>
struct C<Head> : public Head<1> { };
Run Code Online (Sandbox Code Playgroud)
(这是用Clang 3.0编译的)
现在整段代码:
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;
}
Run Code Online (Sandbox Code Playgroud)