Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Suffix Return Type Syntax

16 views
Skip to first unread message

Kenshin

unread,
Nov 24, 2009, 11:18:37 AM11/24/09
to
Hi. Given the below, to sum all arguments passed,

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! ]

Le Chaud Lapin

unread,
Nov 25, 2009, 9:13:16 AM11/25/09
to
On Nov 24, 10:18 am, 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?
> 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...);
>
> }

Hmm...this syntax looks very foreign to me. Are you sure this is C++?

-Le Chaud Lapin-

Yechezkel Mett

unread,
Nov 25, 2009, 9:23:29 AM11/25/09
to
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

red floyd

unread,
Nov 25, 2009, 1:12:46 PM11/25/09
to
On Nov 25, 6:13 am, Le Chaud Lapin <jaibudu...@gmail.com> wrote:
> On Nov 24, 10:18 am, 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?
> > 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...);
>
> > }
>
> Hmm...this syntax looks very foreign to me. Are you sure this is C++?
>

It's C++0x.

SG

unread,
Nov 26, 2009, 1:07:42 AM11/26/09
to
On 25 Nov., 19:12, red floyd wrote:
> On Nov 25, 6:13 am, Le Chaud Lapin wrote:

> > On Nov 24, 10:18 am, Kenshin wrote:
> > >
> > > template<class T1, class T2>
> > > []sum(T1&& t1, T2&& t2)->decltype(t1 + t2){
> > > return t1 + t2;
> > > }
> >
> > Hmm...this syntax looks very foreign to me. Are you sure this is C++?
>
> 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

Kenshin

unread,
Nov 26, 2009, 11:29:07 AM11/26/09
to
On Nov 26, 9:07 am, SG <s.gesem...@gmail.com> wrote:
> On 25 Nov., 19:12, red floyd wrote:
>
> > On Nov 25, 6:13 am, Le Chaud Lapin wrote:
> > > On Nov 24, 10:18 am, Kenshin wrote:
>
> > > > template<class T1, class T2>
> > > > []sum(T1&& t1, T2&& t2)->decltype(t1 + t2){
> > > > return t1 + t2;
> > > > }
>
> > > Hmm...this syntax looks very foreign to me. Are you sure this is C++?
>
> > 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.

{ 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 :-))

0 new messages