Topic: suffix/deduced return type for functions in C++0x?


Author: Scott Meyers <NeverRead@aristeia.com>
Date: Fri, 22 Jan 2010 12:16:44 CST
Raw View
restor wrote:
> Hi,
> I am not sure what is the status of the "suffix return type" feature
> in C++0x. Is any of the following codes going to be valid?
>
>  template<class T, class U>
>       [] mul(T x, U y) { return x*y; }
>
>  template<class T, class U>
>  auto mul(T x, U y) { return x*y; }
>
>  template<class T, class U>
>       [] mul(T x, U y) ->decltype(x*y) { return x*y; }

My understanding is that these three are not allowed, but I did not look it up.
   Note that lambda expressions are *expressions*, so the first and third code
fragments are attempts to declare a template for an expression.  As far as I
know, using auto in front of a function declaration requires that the
declaration be followed by "->" and the function's return type.

>  template<class T, class U>
>  auto mul(T x, U y) ->decltype(x*y) { return x*y; }

This is fine, I believe, except that usually mul would use std::forward on the
arguments when it called operator*, so the return type expression would
typically be decltype(std::forward<T>(x)*std::forward<U>(y)), I believe.

Scott

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: restor <akrzemi1@gmail.com>
Date: Sat, 23 Jan 2010 11:36:31 CST
Raw View
On 22 Jan, 19:16, Scott Meyers <NeverR...@aristeia.com> wrote:
> restor wrote:
> > Hi,
> > I am not sure what is the status of the "suffix return type" feature
> > in C++0x. Is any of the following codes going to be valid?
>
> >  template<class T, class U>
> >       [] mul(T x, U y) { return x*y; }
>
> >  template<class T, class U>
> >  auto mul(T x, U y) { return x*y; }
>
> >  template<class T, class U>
> >       [] mul(T x, U y) ->decltype(x*y) { return x*y; }
>
> My understanding is that these three are not allowed, but I did not look
it up.
>    Note that lambda expressions are *expressions*, so the first and third
code
> fragments are attempts to declare a template for an expression.  As far as
I
> know, using auto in front of a function declaration requires that the
> declaration be followed by "->" and the function's return type.
>
> >  template<class T, class U>
> >  auto mul(T x, U y) ->decltype(x*y) { return x*y; }
>
> This is fine, I believe, except that usually mul would use std::forward on
the
> arguments when it called operator*, so the return type expression would
> typically be decltype(std::forward<T>(x)*std::forward<U>(y)), I believe.
>
> Scott

Thanks for the reply, I was able to read through the latest draft, and
it says exactly what you did. However, the draft is only a draft, and
there has been an another proposal called "Unified Function Syntax"
that has been developed for a while that proposed extending the
semantics of [] to "normal functions" declarations (rather than using
"auto"), and I believe I even read in one of the minutes that there
was some work on using [] in any declaration whatsoever, to introduce
a new way of linear declaration syntax (rather than convoluted pointer
to function syntax). I was wondering whether this would be going into C
++0x, or has been deferred for later.
Also, the Bjarne Stroustrup's page on C++0x shows an example of how
the "-> decltype()"  can be omitted when the return type can be un-
ambiguously deduced from the sole return statement. It is obviously
useful, even more in your "forward" example. Requiring of me to write:

 template<class T, class U>
 auto mul(T x, U y) -> decltype( std::forward<T>(x) * std::forward<U>
(y) )
 { return std::forward<T>(x) * std::forward<U>(y); }

is cruelty. Also, since C++0x already introduces the requirement (on
the compilers) of analyzing the function body, and  accepting or
rejecting the function based on this analysis (this is done for
constexpr functions and lambdas), the same logic could apply to
function definitions (inferring return type in unambiguous cases). I
was wondering if this is also going into C++0x.

Regards,
&rzej


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: restor <akrzemi1@gmail.com>
Date: Tue, 12 Jan 2010 14:51:15 CST
Raw View
Hi,
I am not sure what is the status of the "suffix return type" feature
in C++0x. Is any of the following codes going to be valid?

 template<class T, class U>
      [] mul(T x, U y) { return x*y; }

 template<class T, class U>
 auto mul(T x, U y) { return x*y; }

 template<class T, class U>
      [] mul(T x, U y) ->decltype(x*y) { return x*y; }

 template<class T, class U>
 auto mul(T x, U y) ->decltype(x*y) { return x*y; }

Regards,
&rzej

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]