Rvalue References: Because C++ is not Complicated Enough

about | archive


[ 2008-March-11 18:43 ]

I wish this were an April fool's joke, but it is real: A Brief Introduction to Rvalue References describes a new feature of C++0x, the proposed next version of the C++ standard. Rvalue references permit references to be bound to temporaries (rvalues). The following is not possible with old-fashioned C++ references:

A&& a_ref = A();

The article describes how this permits "move semantics." For example, today if you say std::vector<T> v1 = v2 you get a copy operation. What if you want a move instead? The article presents many examples of how to use rvalue references to do that.

However, the result is insane. It introduces more syntax that C++ programmers will need to learn. The differences between rvalue references and lvalue references are subtle, and will likely be confusing. It certainly confuses me a little.

I propose the following alternative: use a pointer. In this case, a move is simple: A* a = A(). Or add a swap member function. This is not "elegant" nor is it as general. However, this is what we've been using for decades. It also requires zero changes to C++ compilers, and requires users to learn nothing. It has been immortalized in books like Effective C++ (Item 25: Consider support for a non-throwing swap).

Does the standards committee not realize that C++ is already the most complicated programming language that is widely used? Part of the benefit of Java is that it is (or was) simpler than C++. Work like this is harmful to the longevity of the language, as it makes it even harder for new programmers to understand the code that their predecessors wrote. If this continues, at some point C++ may be a lost cause, and some other language will need to replace it as a low-level "systems" language (D? Java? Something else?). As a side note, it turns out that GCC already supports this ridiculousness.