This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

implementing rvalue references in g++

[sorry for the duplicate post, the first one somehow didn't have line breaks]


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]