在嵌套模板专门化期间初始化嵌套模板类的静态成员? [英] Initializing static member of nested templated class during nested template specialization?

查看:177
本文介绍了在嵌套模板专门化期间初始化嵌套模板类的静态成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的问题:

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

如何正确初始化?

推荐答案

完全专用的模板实际上不再是模板,因此您的定义应该是:

Fully-specialized templates are in fact no longer templates, so your definition should simply be:

int Outer<bool>::Inner<bool>::count = 4;

完全使用所有定义后,代码应如下所示:

In full, with all definitions in place, your code should look like:

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;

这篇关于在嵌套模板专门化期间初始化嵌套模板类的静态成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆