문제

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.

도움이 되었습니까?

해결책

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. */
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top