Rvalue References Overview
Rvalue references, introduced in C++11, use the syntax T&&. They allow functions to distinguish between lvalues and rvalues, enabling move semantics.
main.cpp
12345678910111213141516171819#include <iostream> void process(int& x) { std::cout << "Lvalue reference called with " << x << std::endl; } void process(int&& x) { std::cout << "Rvalue reference called with " << x << std::endl; } int main() { int a = 42; process(a); // lvalue, calls process(int&) process(99); // rvalue, calls process(int&&) process(std::move(a)); // rvalue, calls process(int&&) }
Notice in the example above how the function overloads are selected based on whether the argument is an lvalue or an rvalue. This distinction is the foundation for move semantics.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Чудово!
Completion показник покращився до 10
Rvalue References Overview
Свайпніть щоб показати меню
Rvalue references, introduced in C++11, use the syntax T&&. They allow functions to distinguish between lvalues and rvalues, enabling move semantics.
main.cpp
12345678910111213141516171819#include <iostream> void process(int& x) { std::cout << "Lvalue reference called with " << x << std::endl; } void process(int&& x) { std::cout << "Rvalue reference called with " << x << std::endl; } int main() { int a = 42; process(a); // lvalue, calls process(int&) process(99); // rvalue, calls process(int&&) process(std::move(a)); // rvalue, calls process(int&&) }
Notice in the example above how the function overloads are selected based on whether the argument is an lvalue or an rvalue. This distinction is the foundation for move semantics.
Дякуємо за ваш відгук!