mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-16 20:27:08 +03:00
39 lines
1.2 KiB
C++
39 lines
1.2 KiB
C++
//
|
|
// 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 = lv1; // 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;
|
|
}
|