Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

2014 05 27 Lvalue and Rvalue

krikit edited this page Nov 26, 2014 · 1 revision

Lvalue and Rvalue

Lvalue는 식을 벗어나도 유효한 값을 말하고, Rvalue는 식을 벗어나면 사라져 버리는 임시 값을 말합니다. 즉, x = y + 1과 같은 식에서 x는 Lvalue, y + 1은 Rvalue입다.

Lvalue에는 값의 할당이 가능하지만, Rvalue에는 그렇게 할 수 없습니다.

int x = 1;    // 맞는 식
y + 1 = 2;    // 틀린 식

가장 확실하게 구분할 수 있는 방법은 Lvalue는 주소 참조가 가능하지만, Rvalue에는 그렇게 할 수 없습니다.

int* x_ptr = &x;          // 맞는 식
int* y_ptr = &(y + 1);    // 틀린 식

이에 대한 자세한 내용은 다음을 참고하시기 바랍니다. http://msdn.microsoft.com/ko-kr/library/f90831hc.aspx

C++11에서부터 Rvalue reference가 생겼습니다. 기존의 int&와 같은 reference는 무조건 Lvalue reference였는데요, int&&와 같이 Rvalue도 reference를 갖게 되었습니다.

int x = 1
int& x_ref = x;         // Lvalue reference
int& x_ref = 1;         // 틀린 식 (Rvalue를 Lvalue reference에 대입)
int&& y_ref = x + 1;    // Rvalue reference
int&& y_ref = x;        // 틀린 식 (Lvalue를 Rvalue reference에 대입)

Rvalue reference와 move semantics로 인해 STL에서 대형 컬렉션을 함수의 인자나 리턴 값으로 넘길 때 불필요한 복사가 많이 줄어들었다고 합니다. 자세한 내용은 다음 링크를 참고하시기 바랍니다. http://www.devbb.net/viewtopic.php?f=21&t=57

Clone this wiki locally