Re: Suffix Return Type Syntax
On Nov 24, 6:18 pm, Kenshin <kenshin.himura.sakab...@gmail.com> wrote:
Hi. Given the below, to sum all arguments passed,
1. What should be coded in place of the "/*What goes here*/" part?
template<class T1, class T2>
[]sum(T1&& t1, T2&& t2)->decltype(t1 + t2){
return t1 + t2;
}
template<class T1, class T2, class... T3>
[]sum(const T1& t1, const T2& t2, const T3&... t3)->decltype(/*What
goes here*/){
return t1 + sum(t2, t3...);
}
Won't this work:
template<class T1, class T2, class... T3>
auto sum(const T1& t1, const T2& t2, const T3&... t3)
->decltype(t1 + sum(t2, t3...))
{
return t1 + sum(t2, t3...);
}
2. How can I modify this to use std::plus<>() instead?
The problem with using std::plus<> is that it requires both arguments
to be the same type, while your function doesn't.
Otherwise you could just use it:
template<class T1, class T2, class... T3>
auto sum(const T1& t1, const T2& t2, const T3&... t3)
->decltype(std::plus<T1>()(t1, sum(t2, t3...)))
{
return std::plus<T1>()(t1, sum(t2, t3...));
}
Yechezkel Mett
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]