BCC32, which is the classic C++Builder 32 bit compiler, includes the use of rvalue references, which allow creating a reference to temporaries. Also, rvalue references avoid unnecessary copying and make possible perfect forwarding functions. This feature is one of the C++11 features.
Rvalue references are a compound type like standard C++ references, which are referred to as lvalue references. An lvalue reference is formed by appending the ampersand character (&) to a type:
1 2 |
SomeClass l; SomeClass& lReference = l; //lvalue reference |
The syntax of an rvalue reference is to add && after a type:
1 2 3 |
SomeClass r; SomeClass&& rReference = r; //rvalue reference |
An rvalue reference behaves like an lvalue reference, except that you can bind an rvalue reference to a temporary — an rvalue.
1 2 3 4 5 6 7 8 9 |
SomeClass a; a = SomeClass(); SomeClass& lReference = a; //OK-lvalue reference can bind to an lvalue such as "a" SomeClass& lReference2 = SomeClass(); //error-lvalue reference can't bind to an rvalue SomeClass&& rReference = SomeClass(); //OK for rvalue reference to bind to rvalue // Both references can be used the same way SomeOtherClass value1 = SomeFunc(lReference); SomeOtherClass value2 = SomeFunc(rReference); |
In the example above, SomeClass() is not bound to an identifier, so it is an rvalue and can be bound to an rvalue reference — but not an lvalue reference.
Here is the above sample code in C++Builder.
Up next: whether you’re a beginner or a professional, this compiler explorer tutorial will help you remember the language features essential in C++ programming.
Find out more about C++ rvalue References over in the Embarcadero DocWiki.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition