2
可能重複:
trailing return type using decltype with a variadic template function遞歸追溯返回類型?
我要作出這樣的總結了幾個值的函數。如果我不使用尾隨返回類型,則count()
將使用第一個參數的類型,因爲它是返回類型。但是,使用後返回類型時,我不能讓代碼編譯:
#include <iostream>
template<typename T>
inline T sum(T a) {
return a;
}
template<typename T, typename... Args>
inline auto sum(T a, Args... b) -> decltype(a + sum(b...)) {
return (a + sum(b...));
}
int main() {
std::cout << sum(1, 2.5f, 3, 4);
return 0;
}
錯誤(GCC)是:
main.cpp: In function ‘int main()’:
main.cpp:16:30: error: no matching function for call to ‘sum(int, float, int, int)’
main.cpp:16:30: note: candidates are:
main.cpp:5:10: note: template<class T> T sum(T)
main.cpp:10:13: note: template<class T, class ... Args> decltype ((a + sum(sum::b ...))) sum(T, Args ...)
我怎樣才能得到這個工作?
是不是'count'應該是'sum'? –
int main()與有問題的行16在哪裏? http://sscce.org/ – moshbear
Works here:http://ideone.com/v8nJK – jpalecek