Hi,
I just wanted to let anyone who might interested know that there's an attempt
underway to add support for rvalue references to G++. I've got a preliminary
patch at http://russ.hn.org/rref/ which adds partial support for them, and I'm
hoping to turn it into a complete implementation over the next few weeks.
Rvalue references are described and proposed for inclusion in a future C++
standard in a paper [1] by Peter Dimov, Howard Hinnant, and Dave Abrahams. They
work pretty much the same way as normal C++ references, the main difference
being that they are allowed to bind to temporary objects that hold the results
of rvalue expressions. Example:
struct S{};
S foo();
// invalid in C++, normal non-const references can't bind to rvalues
S & lvalue_ref = foo();
// rvalue references are declared with && and can bind to rvalues
S && rvalue_ref = foo();
The situations that require rvalue references are complicated, and amply
described in [1] and [2]. But the basic idea is that when you have a modifiable
reference to an temporary, you can work with it directly instead of having to
create unnecessary copies of it. Generic code can use them to work well with
objects that may be expensive to copy (like strings, containers) or impossible
to copy (like auto_ptrs).
The changes that need to be made to the compiler to support rvalue references
are straightforward and are laid out in [1] and [3]. The compiler needs to be
able to understand the && syntax for rvalue reference declarations, and to
follow some new rules for && types when doing things like resolving function
overloads and performing template type deduction.
Rvalue references are backwards compatible, and do not in affect the
compilation or behavior of existing C++ code. They have already been
implemented in Metrowerks Codewarrior, and according to Howard Hinnant [4],
they will be included in an upcoming release of that compiler (along with
standard library extensions that take advantage of them for performance and
usability gains).
- Russ
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm
[2] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1690.html
[3] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1770.html
[4]
http://groups-beta.google.com/group/comp.std.c++/browse_thread/thread/64bf775bdf069dad