类模板专用化中的成员函数语法



我有一个类模板,我们称之为A,它有一个成员函数abc()

template <typename T>
class A{
public:
    T value;
    void abc();
};

我可以使用以下语法在类声明之外实现成员函数abc()

template <typename T>
void A<T>::abc()
{
    value++;
}

我想做的是为这个类创建一个模板专业化,比如说int.

template <>
class A<int>{
public:
    int value;
    void abc();
};

问题是:为专用类实现abc()的正确语法是什么?

我尝试使用以下语法:

template <>
void A<int>::abc()
{
   value += 2;
}

但是,这不会编译。

void A<int>::abc()
{
   value += 2;
}

由于A<int>A<T> explicit specialisation.

http://liveworkspace.org/code/982c66b2cbfdb56305180914266831d1

N3337 14.7.3/5

显式专用类模板的成员包括定义方式与普通类的成员相同,并且不使用模板<>语法

[ 示例:

template<class T> struct A {
struct B { };
template<class U> struct C { };
};
template<> struct A<int> {
void f(int);
};
void h() {
A<int> a;
a.f(16);
}
// A<int>::f must be defined somewhere
// template<> not used for a member of an
// explicitly specialized class template
void A<int>::f(int) { /∗ ... ∗/ }

删除template<>

void A<int>::abc()
{
   value += 2;
}

最新更新