Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Rvalue and Lvalue References | References Fundamentals
C++ Pointers and References

book
Rvalue and Lvalue References

Concepts lvalue and rvalue are related to the classification of expressions.

  • lvalue or left value: is an expression that refers to an object with identifiable memory location. It represents an object that can be modified;

  • rvalue or right value: is an expression that represents a temporary or disposable value.

Temporary Values

A temporary value is a short-lived, intermediate result created during the evaluation of an expression.

rvalue.h

rvalue.h

copy
// `(27 + 6 + 3 + 2)` is a tempopary value
int sum = 27 + 6 + 3 + 2;

// `static_cast<float>(sum)` is a tempopary value
float avarage = static_cast<float>(sum) / count;

// std::max(7, 9) will return a tempopary value
int largest = std::max(7, 9);
12345678
// `(27 + 6 + 3 + 2)` is a tempopary value int sum = 27 + 6 + 3 + 2; // `static_cast<float>(sum)` is a tempopary value float avarage = static_cast<float>(sum) / count; // std::max(7, 9) will return a tempopary value int largest = std::max(7, 9);

Temporary values are automatically managed by the compiler, and they exist for the duration of the expression or operation where they are created. After that, they are typically discarded, and the result is stored in the target variable or used as needed.

Move semantics

An rvalue reference is denoted by the double ampersand (&&).

The only difference between an lvalue and rvalue reference is that rvalue can be bound to a temporary object, whereas an lvalue reference cannot.

Note

Using an rvalue reference in this context might not be very practical, as there is no benefit to using an rvalue reference for a simple literal

A combination of lvalue and rvalue references is used to support move semantics, allowing resources to be transferred from one object to another without unnecessary copying. Look at the example below:

swap.h

swap.h

copy
std::string swap(std::string& a, std::string& b)
{
std::string tmp(a); // We have two copies of string `a`
a = b; // Now we have two copies of string `b`
b = tmp; // And now we have two copies of string `tmp`
}
123456
std::string swap(std::string& a, std::string& b) { std::string tmp(a); // We have two copies of string `a` a = b; // Now we have two copies of string `b` b = tmp; // And now we have two copies of string `tmp` }

But we don't need copies of a or b. We simply wanted to swap them. Let's try again.

main.cpp

main.cpp

copy
#include <iostream>

void swap(std::string &a, std::string &b)
{
// Move the content of a into temp
std::string temp(std::move(a));
// Move the content of b into a
a = std::move(b);

// Move the content of temp into b
b = std::move(temp);
}

int main()
{
std::string a = "Hello\n";
std::string b = "Bye\n";

swap(a, b);

std::cout << a << b;
}
1234567891011121314151617181920212223
#include <iostream> void swap(std::string &a, std::string &b) { // Move the content of a into temp std::string temp(std::move(a)); // Move the content of b into a a = std::move(b); // Move the content of temp into b b = std::move(temp); } int main() { std::string a = "Hello\n"; std::string b = "Bye\n"; swap(a, b); std::cout << a << b; }

std::move(): transforms an lvalue into an rvalue reference, enabling move semantics and the transfer or ownership without copying.

question mark

What is an lvalue?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt