cod*_*ddy 1 c++ static templates
这是我的问题:
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
Run Code Online (Sandbox Code Playgroud)
如何正确初始化?
完全专业化的模板实际上不再是模板,因此您的定义应该只是:
int Outer<bool>::Inner<bool>::count = 4;
Run Code Online (Sandbox Code Playgroud)
完整的,所有定义到位后,您的代码应如下所示:
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;
Run Code Online (Sandbox Code Playgroud)