问题
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