Search⌘ K

- Example

Explore the use of rvalue and lvalue references as function arguments in C++. Understand how named variables behave as lvalues and temporary objects as rvalues, and how std::move converts lvalues to rvalues. This lesson helps you grasp the foundation of move semantics important for efficient resource management in C++.

Rvalue/Lvalue references as arguments #

C++
#include <algorithm>
#include <iostream>
#include <string>
struct MyData{};
std::string function( const MyData & ) {
return "lvalue reference";
}
std::string function( MyData && ) {
return "rvalue reference";
}
int main(){
std::cout << std::endl;
MyData myData;
std::cout << "function(myData): " << function(myData) << std::endl;
std::cout << "function(MyData()): " << function(MyData()) << std::endl;
std::cout << "function(std::move(myData)): " << function(std::move(myData)) << std::endl;
std::cout << std::endl;
}
...