是否可以使用参数为返回值专门化模板?我在尝试执行下面显示的模板特化时遇到错误。所以我现在将这两个专业声明为使用宏来避免重复代码的不同功能。返回类型参数的函数模板专门化
#include <iostream>
template<class T1,class T2>
inline T1 func(const T2& a) { return T1(3.5);}
template<>
inline float func(const int& a) { return (1.0); }
template<>
inline double func(const float& a) {return (2.0); }
int main() {
func(2);
return 0;
}
的错误是:
temp.cpp:13:3: error: no matching function for call to 'func'
func(2);
^~~~
temp.cpp:4:11: note: candidate template ignored: couldn't infer template argument 'T1'
inline T1 func(const T2& a) { return T1(3.5);}
^
1 error generated.
你会得到什么错误? – user463035818
为什么不只是一个超负荷? – AndyG
[无法重现](http://coliru.stacked-crooked.com/a/fbdd4603f59a6613)。 –