In C++, values represent data that variables hold or expressions produce during a program's execution. These values can be categorized based on how they behave, how they are stored, and how they can be used. Understanding the types of values helps write more efficient, safe, and readable code.
1. Lvalue (Left Value)
An lvalue (locator value) refers to an object that occupies some identifiable location in memory. You can take the address of an lvalue using the & operator, and it can appear on the left-hand side or right-hand side of an assignment.
Example:
C++
#include <iostream>
using namespace std;
int main() {
// x is an lvalue
int x = 10;
// valid: lvalue on left
x = 20;
cout << x;
return 0;
}
The lvalues has a persistent address in memory and can be assigned a value (if not const).
2. Rvalue (Right Value)
An rvalue is a value that does not have a persistent memory address (or it's not safe/allowed to take its address). Rvalues typically appear on the right-hand side of an assignment.
Example:
C++
#include <iostream>
using namespace std;
int main() {
// 5 + 3 is an rvalue
int y = 5 + 3;
// y is lvalue, but its value is rvalue
int z = y;
cout << y << " " << z;
return 0;
}
The rvalues represents temporary values. Cannot appear on the left-hand side of assignment.
3. Prvalue (Pure Rvalue)
A prvalue (pure rvalue) is a type of rvalue that represents temporary values that don’t have an identity (like literals or the result of most expressions).
Example:
C++
#include <iostream>
using namespace std;
int main() {
// 100 is prvalue
int a = 100;
// (a + 20) is prvalue
int b = a + 20;
cout << a << " " << b;
return 0;
}
The prvalues are used to initialize objects. They represents literal values, temporary results of expressions, or returned values from functions that return by value.
Xvalue (Expiring Value)
An xvalue (expiring value) is a type of rvalue that represents an object near the end of its lifetime. It typically refers to resources that can be moved from.
Example
C++
#include <iostream>
using namespace std;
int main() {
// num is glvalue
int num = 42;
cout << num << endl;
return 0;
}
The xvalues are used in move semantics that allows transferring resources instead of copying.
Glvalue (Generalized Lvalue)
A glvalue (generalized lvalue) refers to any value that has an identifiable location in memory (includes both lvalues and xvalues).
Example:
C++
#include <iostream>
using namespace std;
int main() {
// num is glvalue
int num = 42;
cout << num << endl;
return 0;
}
The glvalue has memory identity and can be either lvalue or xvalue.
Similar Reads
Overflow of Values While Typecasting in C ++ C++ datatypes have a specific size, that is, the number of bits that each of them occupies is fixed. Sometimes while writing code, we may end up assigning a value that is greater than the maximum capacity of the datatype to store. In that case, an overflow occurs, and depending on the datatype the c
3 min read
priority_queue value_type in C++ STL The priority_queue :: value_type method is a built-in function in C++ STL which represents the type of object stored as an element in a priority_queue. It acts as a synonym for the template parameter. Time complexity: O(1)Syntax: priority_queue::value_type variable_name It has no parameters and no r
2 min read
typedef in C++ The typedef keyword in C++ is used for aliasing existing data types, user-defined data types, and pointers to a more meaningful name. Typedefs allow you to give descriptive names to standard data types, which can also help you self-document your code. Mostly typedefs are used for aliasing, only if t
5 min read
Literals in C++ In C++ programming language, literals are fundamental elements used to represent fixed values. These values can include numbers, characters, strings, and more. They are generally present as the right operand in the assignment operation.Let's take a look at an example:C++#include <iostream> usi
6 min read
Literals in C In C, Literals are the constant values that are assigned to the variables. Literals represent fixed values that cannot be modified. Literals contain memory but they do not have references as variables. Generally, both terms, constants, and literals are used interchangeably. For example, âconst int =
4 min read