Click here to Skip to main content
Click here to Skip to main content

Tagged as

C++ Tip : Binding a Reference to an Rvalue

, 23 Jun 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Rvalues and lvalues are a fundamental concept of C++ programming. In essence, an rvalue is an expression that cannot appear on the left-hand side of an assignment expression. By contrast, an lvalue refers to an object (in its wider sense), or a chunk of memory, to which you can write a value. References can be bound to both rvalues and lvalues. However, due to the language’s restrictions regarding rvalues, you have to be aware of the restrictions on binding references to rvalues, too.
Binding a reference to an rvalue is allowed as long as the reference is bound to a const type. The rationale behind this rule is straightforward: you can’t change an rvalue, and only a reference to const ensures that the program doesn’t modify an rvalue through its reference. In the following example, the function f() takes a reference to const int:
 
void f(const int & i);  
int main()  
{  
 f(2); /* OK */  
}  
The program passes the rvalue 2 as an argument to f(). At runtime, C++ creates a temporary object of type int with the value 2 and binds it to the reference i. The temporary and its reference exist from the moment f() is invoked until it returns; they are destroyed immediately afterwards. Note that had we declared the reference i without the const qualifier, the function f() could have modified its argument, thereby causing undefined behavior. For this reason, you may only bind references to const objects.
The same rule applies to user-defined objects. You may bind a reference to a temporary object only if it’s const:
 
struct A{};  
void f(const A& a);  
int main()  
{  
 f(A()); /* OK, binding a temporary A to a const reference*/  
}  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

elitehussar

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 23 Jun 2010
Article Copyright 2010 by elitehussar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid