mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
38
code/3/3.3.rvalue.cpp
Normal file
38
code/3/3.3.rvalue.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// 3.3.rvalue.cpp
|
||||
// modern c++ tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
// https://github.com/changkun/modern-cpp-tutorial
|
||||
//
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
void reference(std::string& str) {
|
||||
std::cout << "lvalue" << std::endl;
|
||||
}
|
||||
void reference(std::string&& str) {
|
||||
std::cout << "rvalue" << std::endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string lv1 = "string,"; // lv1 is a lvalue
|
||||
// std::string&& r1 = s1; // illegal, rvalue can't ref to lvalue
|
||||
std::string&& rv1 = std::move(lv1); // legal, std::move can convert lvalue to rvalue
|
||||
std::cout << rv1 << std::endl; // string,
|
||||
|
||||
const std::string& lv2 = lv1 + lv1; // legal, const lvalue reference can extend temp variable's lifecycle
|
||||
// lv2 += "Test"; // illegal, const ref can't be modified
|
||||
std::cout << lv2 << std::endl; // string,string
|
||||
|
||||
std::string&& rv2 = lv1 + lv2; // legal, rvalue ref extend lifecycle
|
||||
rv2 += "string"; // legal, non-const reference can be modified
|
||||
std::cout << rv2 << std::endl; // string,string,string,
|
||||
|
||||
reference(rv2); // output: lvalue
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user