是否可以使用參數爲返回值專門化模板?我在嘗試執行下面顯示的模板特化時遇到錯誤。所以我現在將這兩個專業聲明爲使用宏來避免重複代碼的不同功能。返回類型參數的函數模板專門化
#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)。 –