[C++11] recursive late-specified return type
Nathan Ridge
zeratul976@hotmail.com
Mon Nov 7 07:41:00 GMT 2011
Hi,
Could someone please clarify whether the following code (from [1]),
which GCC trunk rejects, is valid (i.e. the error is a bug) or invalid?
#include <iostream>
using namespace std;
template <class T>
T sum(const T& in)
{
return in;
}
template <class T, class... P>
auto sum(const T& t, const P&... p) -> decltype(t + sum(p...))
{
return t + sum(p...);
}
int main()
{
cout << sum(5, 10.0, 22.2) << endl;
}
Errors:
test.cpp: In function 'int main()':
test.cpp:18:29: error: no matching function for call to 'sum(int, double, double)'
test.cpp:18:29: note: candidates are:
test.cpp:5:3: note: template<class T> T sum(const T&)
test.cpp:5:3: note: template argument deduction/substitution failed:
test.cpp:18:29: note: candidate expects 1 argument, 3 provided
test.cpp:11:6: note: template<class T, class ... P> decltype ((t + sum(sum::p ...))) sum(const T&, const P& ...)
test.cpp:11:6: note: template argument deduction/substitution failed:
test.cpp: In substitution of 'template<class T, class ... P> decltype ((t + sum(p ...))) sum(const T&, const P& ...) [with T = int; P = {double, double}]':
test.cpp:18:29: required from here
test.cpp:11:6: error: no matching function for call to 'sum(const double&, const double&)'
test.cpp:11:6: note: candidate is:
test.cpp:5:3: note: template<class T> T sum(const T&)
test.cpp:5:3: note: template argument deduction/substitution failed:
test.cpp:11:6: note: candidate expects 1 argument, 2 provided
Thanks,
Nate
[1] http://stackoverflow.com/questions/8023332/recursive-trailing-return-type)
More information about the Gcc-help
mailing list