모든 C++식에는 형식이 있으며 값 범주에 속한다. 식 평가 중에 임시 개체를 만들고 복사하고 이동할 때 컴파일러가 따라야 하는 규칙의 기초이다
#include <iostream>
#include <string>
#include <variant>
void value()
{
int x = 10; // 이름이 있는 객체임으로 glvalue
int &ref = x; // 참조도 glvalue
}
std::variant<int, std::string> prvalue()
{
int x = 10; // 이름이 있는 객체임으로 glvalue
int y = 20; // 이름이 있는 객체임으로 glvalue
std::string str = "hello"; // 이름이 있는 객체임으로 glvalue
return str; // str은 prvalue
return x + y; // x+y는 prvalue
}
void xvalue()
{
std::string str = "this is a glvalue"; // 이름이 있는 객체임으로 glvalue
std::string &&xref = std::move(str); // std::move(str)는 xvalue
}
void rvalue()
{
int sum = 3 + 4; // 3 + 4는 prvalue, 즉 rvalue입니다.
std::string s = "Test"; // "Test"는 prvalue, 즉 rvalue입니다.
std::string s2 = s; // s는 lvalue입니다.
std::string s2 = std::move(s); // std::move(s)는 xvalue, 즉 rvalue입니다.
}