user1899020
user1899020

Reputation: 13575

how to specialize template member functions in template class?

For example:

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

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

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

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

Upvotes: 1

Views: 90

Answers (1)

Andy Prowl
Andy Prowl

Reputation: 126542

First of all, you forgot to specify the return type of the function (void). Secondly, you need to have two template<>: one because you are explicitly specializing the class template, and one because you are explicitly specializing its member function template.

Therefore, this is the correct syntax:

template<> // Because you are explicitly specializing the A class template
template<> // Because you are explicitly specializing the `Fun()` member template
void A<1>::Fun<int>()
{
    /* some code here. */
}

Upvotes: 5

Related Questions