C++ g++;可变模板。简单示例代码赢得';t编译、投诉和#x27;不是模板';

C++ g++;可变模板。简单示例代码赢得';t编译、投诉和#x27;不是模板';,c++,c++11,g++,variadic-templates,C++,C++11,G++,Variadic Templates,我在这片陌生的土地上闲逛,我想我应该尝试一个简单的例子。代码非常简单: template<> struct Count<> { static const int value = 0;}; template<typename T, typename... Args> struct Count<T, Args...> //partial specialization { static const int value = 1 + Co

我在这片陌生的土地上闲逛,我想我应该尝试一个简单的例子。代码非常简单:

template<> struct Count<> { static const int value = 0;};

template<typename T, typename... Args>
    struct Count<T, Args...> //partial specialization
{ 
    static const int value = 1 + Count<Args...>::value;
};
我缺少什么?

模板结构计数{static const int value=0;};
template<> struct Count<> { static const int value = 0;};
是一个没有模板参数的模板专门化。但是,不能专门化尚未是非专门化模板的模板类。换句话说,您必须首先建立模板类的非专门化版本,然后才能专门化它。例如,如果你这样做了:

//non-specialized template
template<typename... Args>
struct Count;

//specialized version with no arguments
template<> struct Count<> { static const int value = 0;};

//another partial specialization
template<typename T, typename... Args>
struct Count<T, Args...> 
{ 
    static const int value = 1 + Count<Args...>::value;
};
//非专用模板
模板
结构计数;
//没有参数的专用版本
模板结构计数{static const int value=0;};
//另一部分专业化
模板
结构计数
{ 
静态常量int value=1+Count::value;
};

那就行了。

因为不打算使用它,所以应该定义非专用的:即
struct Count嗯,是的,那会而且确实有效。感谢您的澄清,回想起来这是很明显的。很明显,原作者认为不需要包括在内。
//non-specialized template
template<typename... Args>
struct Count;

//specialized version with no arguments
template<> struct Count<> { static const int value = 0;};

//another partial specialization
template<typename T, typename... Args>
struct Count<T, Args...> 
{ 
    static const int value = 1 + Count<Args...>::value;
};