这是我的问题:
template<typename T>
class Outer
{
public:
template<typename U>
class Inner
{
private:
static int count;
};
static int code;
void print() const
{
std::cout << "generic";
}
};
template<>
template<>
class Outer<bool>::Inner<bool>
{
static int count;
};
template<>
template<>
int Outer<bool>::Inner<bool>::count = 4; // ERROR
如何正确初始化?
答案 0 :(得分:5)
完全专业化的模板实际上不再是模板,因此您的定义应该只是:
int Outer<bool>::Inner<bool>::count = 4;
完整的,所有定义到位后,您的代码应如下所示:
template<typename T>
class Outer
{
public:
template<typename U>
class Inner
{
private:
static int count;
};
static int code;
void print() const
{
std::cout << "generic";
}
};
template<typename T>
int Outer<T>::code = 0;
template<typename T>
template<typename U>
int Outer<T>::Inner<U>::count = 0;
template<>
template<>
class Outer<bool>::Inner<bool>
{
static int count;
};
int Outer<bool>::Inner<bool>::count = 4;
答案 1 :(得分:0)
要为每个Inner模板类实例获取一个静态成员,您可以选择使用单独的模板类。
template<typename T>
class Outer
{
public:
template<typename U>
class Inner
{
};
};
template <typename T>
struct InnerStatic
{
static int count;
};
template <typename T>
int InnerStatic<T>::count = 4;
int main(int argc, char** argv)
{
std::cout << InnerStatic<Outer<int>::Inner<int>>::count << std::endl;
return 0;
}