Jump to content
Nytro

C++ Rvalue References Explained

Recommended Posts

Posted

[h=2]C++ Rvalue References Explained[/h]By Thomas Becker

Last updated: September 2011


[h=3]Contents[/h]

  1. Introduction
  2. Move Semantics
  3. Rvalue References
  4. Forcing Move Semantics
  5. Is an Rvalue Reference an Rvalue?
  6. Move Semantics and Compiler Optimizations
  7. Perfect Forwarding: The Problem
  8. Perfect Forwarding: The Solution
  9. Rvalue References and Exceptions
  10. The Case of the Implicit Move
  11. Acknowledgments and Further Reading

[h=4]Introduction[/h] Rvalue references are a feature of C++ that was added with the C++11 standard. Rvalue references are elusive. I have personally overheard several people with very, very big names in the C++ community say these things: "Everytime I think I have grasped rvalue references, they evade me again."

"Oh those rvalue references. I'm having a real hard time wrapping my head around that."

"I dread having to teach rvalue references."

The nasty thing about rvalue references is that when you look at them, it is not at all clear what their purpose might be or what problems they might solve. Therefore, I will not jump right in and explain what rvalue references are. A better approach is to start with the problems that are to be solved, and then show how rvalue references provide the solution. That way, the definition of rvalue references will appear plausible and natural to you.

Rvalue references solve at least two problems:

  1. Implementing move semantics
  2. Perfect forwarding

If you are not familiar with these problems, do not worry. Both of them will be explained in detail below. We'll start with move semantics. But before we're ready to go, I need to remind you of what lvalues and rvalues are in C++. Giving a rigorous definition is surprisingly difficult, but the explanation below is good enough for the purpose at hand. The original definition of lvalues and rvalues from the earliest days of C is as follows: An lvalue is an expression e that may appear on the left or on the right hand side of an assignment, whereas an rvalue is an expression that can only appear on the right hand side of an assignment. For example,

  int a = 42;
int b = 43;

// a and b are both l-values:
a = b; // ok
b = a; // ok
a = a * b; // ok

// a * b is an rvalue:
int c = a * b; // ok, rvalue on right hand side of assignment
a * b = 42; // error, rvalue on left hand side of assignment

In C++, this is still useful as a first, intuitive approach to lvalues and rvalues. However, C++ with its user-defined types has introduced some subtleties regarding modifiability and assignability that cause this definition to be incorrect. There is no need for us to go further into this. Here is an alternate definition which, although it can still be argued with, will put you in a position to tackle rvalue references: An lvalue is an expression that refers to a memory location and allows us to take the address of that memory location via the & operator. An rvalue is an expression that is not an lvalue. Examples are:

  // lvalues:
//
int i = 42;
i = 43; // ok, i is an lvalue
int* p = &i; // ok, i is an lvalue
int& foo();
foo() = 42; // ok, foo() is an lvalue
int* p1 = &foo(); // ok, foo() is an lvalue

// rvalues:
//
int foobar();
int j = 0;
j = foobar(); // ok, foobar() is an rvalue
int* p2 = &foobar(); // error, cannot take the address of an rvalue
j = 42; // ok, 42 is an rvalue

If you are interested in a rigorous definition of rvalues and lvalues, a good place to start is Mikael Kilpeläinen's ACCU article on the subject.

Sursa: C++ Rvalue References Explained

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...