1. What should be coded in place of the "/*What goes here*/" part?
2. How can I modify this to use std::plus<>() instead?
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...);
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Hmm...this syntax looks very foreign to me. Are you sure this is C++?
-Le Chaud Lapin-
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
It's C++0x.
Not yet. As far as I know the "unified function syntax" proposal
wasn't voted into the standard yet. There are still some concerns.
Frankly, I don't care much about auto vs []. I just hope that for
"normal functions" the trailing return type becomes optional just like
it's the case for single-statement-lambdas already. Also, the "lambda-
to-function-pointer decay" for lambdas with an empty effective capture
set seems to be a nice idea.
Cheers,
SG
{ edits: quoted sig and banner removed. don't quote sigs or the banner. -mod }
Yes, it should be optional, like the lambda syntax.
According to Mr. Stroustrup, the "->decltype(...)" section is
redundant, casue the compiler already knows the return type. The
commitee is still coming to a conclusion on this, but so far, the
"auto" or "[]" at the beginning is for sure (though they still need to
decide which :-))