显式模板特化错误

Explicit template specialization error

这个应该很简单。我正在使用模板,但出现编译错误。

#include <iostream>

template <class T1, class T2>
class Pair
{
    private:
        T1 a;
        T2 b;
    public:
        T1& first();
        T2& second();
        Pair(const T1& aval, const T2& bval) : a(aval), b(bval) {}
};

template <class T1, class T2>
T1& Pair<T1,T2>::first()
{
    return a;
}


template <class T1, class T2>
T2& Pair<T1,T2>::second()
{
    return b;
}

// Explicit Specialization
template <>
class Pair<double, int>
{
    private:
        double a;
        int b;
    public:
        double& first();
        int& second();
        Pair(const double& aval, const int& bval) : a(aval), b(bval) {}
};

template <>
double& Pair<double,int>::first()
{
    return a;
}

template <>
int& Pair<double,int>::second()
{
    return b;
}


int main(int argc, char const *argv[])
{

    Pair<int, int> pair(5,6);
    //Pair<double,int> pairSpec(43.2, 5);
    return 0;
}

错误看起来像这样

main.cpp:42:27: error: no function template matches function template specialization 'first'
double& Pair<double,int>::first()
                          ^
main.cpp:49:24: error: no function template matches function template specialization 'second'
int& Pair<double,int>::second()

任何可能出错的线索?

您不需要在方法声明之前声明模板<>。

double& Pair<double,int>::first() {
    return a;
}
int& Pair<double,int>::second() {
   return b;
}

应该够了。

由于其他答案没有解释为什么这里不需要前缀 template<>,我将尝试在我的答案中提供该解释。

问题的解决方案

如下所述,我们只需要删除 template<> 前缀,如下所示:

//no prefix template<> needed here
inline double& Pair<double,int>::first()
{
    return a;
}

//no prefix template<> needed here
inline int& Pair<double,int>::second()
{
    return b;
}

Working demo

请注意,添加 inline 关键字是为了避免出现多重定义错误,因为通常我们在头文件中实现模板,然后将其包含在多个源文件中。

问题的解释

我们不需要前缀 template<> 的原因是我们正在为一个完整的成员函数提供一个普通的 out-of-class 定义 class 模板专业化。也就是说,我们实际上并没有特化成员函数,而是在 class 定义之外为这些成员函数提供一个普通的 (non-template)。