How to specialize template member functions in template class (already specilzied)?

孤者浪人 提交于 2019-12-12 18:22:35

问题


For example:

template<unsigned number>
struct A
{
    template<class T>
    static void Fun()
    {}
};

template<>
struct A<1>
{
    template<class T>
    static void Fun()
    {
       /* some code here. */
    }
};

And want to specialize A<1>::Fun()

template<>
template<>
void A<1>::Fun<int>()
{
    /* some code here. */
}

doesn't seem to work. How to do it? Thanks.


回答1:


An explicit specialization of a class template is like a regular class (it is fully instantiated, so it is not a parametric type). Therefore, you do not need the outer template<>:

// template<> <== NOT NEEDED: A<1> is just like a regular class
template<> // <== NEEDED to explicitly specialize member function template Fun()
void A<1>::Fun<int>()
{
    /* some code here. */
}

Similarly, if your member function Fun were not a function template, but a regular member function, you would not need any template<> at all:

template<>
struct A<1>
{
    void Fun();
};

void A<1>::Fun()
{
    /* some code here. */
}


来源:https://stackoverflow.com/questions/17503697/how-to-specialize-template-member-functions-in-template-class-already-specilzie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!