Topic: Suggest applying r-value reference template argument deduction to auto(i.e. auto&&)


Author: pongba@gmail.com
Date: Sat, 10 Dec 2005 16:25:11 CST
Raw View
Hi,all

While I've read through almost all the proposals about auto&decltype, I
have some questions and some suggestions. The questions( and maybe some
suggestion) are listed in another post named "Question about
auto&decltype proposal" prior to this one.

In this one, I suggest applying r-value reference template argument
deduction to auto(i.e. auto&&).
Really, the idea is quite simple, it's just I didn't saw it in the
proposals, and I was quite puzzled. Anyway, here's the example code:

int i = 0;
auto&& a = i; // because i is a l-value, auto&& is deduced to int&

this is plain, but what's more useful is that we can use it like this:

// here, if f() returns a l-value, auto&& will be deduced to T&,
otherwise T&&
auto&& ret = f();

and I also suggest restoring the use of auto as return type that is
deduced from the return statement, because by adding '&&' it seems that
auto&& can handle the return-type-l-valueness of forwarding functions
pretty well [ example:

// note that I used auto in parameter type, as I suggest restoring this
usage, too
auto&& fwd(auto t)
{
  f(t);
}
-end example]

Is this neat? Without the redundant, ugly, anti-DRY 'decltype(f(t))'.
Of course I have read N1607 in which Bjarne Stroustrup lists out the
main problems of using auto as return type, but I think for plain
forwarding function templates and short function templates(they exist
widely), using auto&& as their automatic deduced return type works
pretty well and neat - just look how clean they are!
And there's more I would reason about this, I listed 'em in my previous
post named "Question about auto&decltype proposal".

By the way, the reason I suggest restoring the use of auto in the
parameter type is because I think that there's plenty of time when
people just don't care about those unnecessary 'T's or 'U's, and that's
where auto comes in and helps elide many 'template<typename T...>'
coding:-)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]