Why the code below does not compile?
template
T sum(T t){
return t;
}
template
auto sum(T t, U... u
Quoted from [basic.scope.pdecl]:
The point of declaration for a name is immediately after its complete declarator (Clause 8) and before its initializer (if any)
The declaration of the second function template is complete after the trailing return type decltype(t + sum(u...))
. So, when parsing decltype(t + sum(u...))
, the second template is not in scope yet, and the compiler can only see the first template which does not match the call.
One possible fix:
template
struct ReturnType;
template
struct ReturnType {
typedef T Type;
};
template
struct ReturnType {
typedef typename ReturnType::Type Type_;
typedef decltype(std::declval() + std::declval()) Type;
};
template
T sum(T t){
return t;
}
template
typename ReturnType::Type sum(T t, U... u) {
return t + sum(u...);
}