Value categories in C++

Nicola Andrei-George
2 min readSep 23, 2024

--

rvalue, lvalue and the others explained

Historically, when there were only two categories: rvalue and lvalue the names resembled the place in an assignation related to the equal sign, it can be either on the left or on the right.

Furthermore, since C++11 there 3 more categories summing up to five.

value categories inheritance

lvalue

An lvalue(left value) is an expression which refers to a location in memory and may be assigned a value to. Long story short, any kind of expression that may be found on the right side of an assignment is an lvalue.

int x = 12;
int a[] = {5, 2, 4, 10, 2};
x = 30; // x is a left value
a[4] = 6;
*(a+1) = 7; // *(a+1) is left value
// a[] = {5, 7, 4, 10, 6}

rvalue

An rvalue(right value) is any expression which may lay on the right side of the equal sign from an assignment operation.

const int s = 10;
int q = s + 2; // 's + 2' is an rvalue
int w = 3 * 4; // 3*4 is an rvalue
int e = min(q, w); // min(q, w) is an rvalue
int r = max(q, w); // max(q, w) is an rvalue

xvalue

The name xvalue(expiring value) comes from expiring because of its temporarily state of being. Case in point, function return value or an object’s method.

int age(){
return 14;
}
char firstLetter() {
return 'c';
}
// main:
a = age(); // xvalue
c = firstLetter(); // xvalue

glvalue

A glvalue is a generalised value, which is either an lvalue or an xvalue. How is that possible?

int&& foo() {
int x = 10;
return std::move(x);
}
int&& result = foo(); // this is an xvalue, thus also a glvalue
int a = 5;
int* p = &a;
*p = 6; // this is an lvalue, thus also a glvalue

prvalue

A prvalue is a pure value and explicitly is a rvalue which is not an xvalue. For instance, a variable may be must be assigned with a literal like 5, or 54.23 or 3.14 or ‘c’ or This is a string” or with a function call which is temporarily stored and then wiped out. An xvalue is an rvalue which is a function call, but a prvalue is a rvalue which is a literal.

int a = 12; // 12 is a prvalue
string name = "Thomas"; // "Thomas" is a prvalue

Story end

I hope you liked the article and feel freely to comment anything you like and if you have ideas of any other articles you would like to read please let me know :)

--

--

Nicola Andrei-George
Nicola Andrei-George

Written by Nicola Andrei-George

just your regular computer science student passionate about AI and satellite communication. You can contact me at nicola.andrei.g@gmail.com

No responses yet