c++ 显式专业化无法正常工作 VS Studio



我在Visual Studio 2017中工作,并在编译时注意到一个错误

template<class T>T add(T a, T b)
{
return a + b;
};
template<> Line add<Line, Point>(Line line, Point point) /*E0493 there are 
no instances of the "add" function pattern corresponding to the specified  
type*/ 
{
Line newline;
newline.start = add(point, line.start);
newline.end = add(point, line.end);
return newline;
}
template<> Point add<Point, Point>(Point a, Point b)//here is the same
{
Point res;
res.x = a.x + b.x;
res.y = a.y + b.y;
return res;
}

我试图在add((函数和代码的第二个和第三个实现工作正常之前擦除"模板<>"。但是我想了解为什么此代码在我的VS Studio 2017中不起作用。

函数模板专用化的正确语法是

template<> Point add(Point a, Point b) { ... }

然而,add(Line, Point)不可能是主模板template<class T> T add(T a, T b)的特化,因为后者需要两个相同类型的参数。

最新更新