A prvalue (short for "pure rvalue") in C++ is a type of value that represents a temporary value or an expression that does not have an associated memory location. Prvalues are often the result of expressions that produce temporary objects, literals, or intermediate values. Prvalues are a fundamental concept in C++'s value category system, which helps the language define how different expressions behave, especially in terms of memory management and optimization.
&
operator.In C++, expressions are classified into three main categories: - glvalue (generalized lvalue): - lvalue: An expression that refers to an object with a persistent memory location. - xvalue: An expression that refers to an object that is about to be moved from or destroyed. - prvalue (pure rvalue): A temporary value that does not refer to a memory location. Prvalues typically result from literal values, temporary objects, or function calls.
#include <iostream>
int main() {
int a = 10; // '10' is a prvalue
int b = a + 5; // 'a + 5' is a prvalue
std::cout << "b: " << b << std::endl;
return 0;
}
10
: This is a prvalue because it is a literal value that does not refer to any specific memory location.a + 5
: This expression results in a prvalue because it produces a temporary value that is used to initialize b
.Prvalues often appear as return values from functions or as temporary objects passed to functions.
#include <iostream>
int getValue() {
return 42; // '42' is a prvalue
}
int main() {
int x = getValue(); // 'getValue()' returns a prvalue
std::cout << "x: " << x << std::endl;
return 0;
}
42
: The literal 42
is a prvalue, and since it does not have a persistent memory location, it is returned directly by the getValue()
function.getValue()
: The return value of getValue()
is a prvalue, which is used to initialize x
.Prvalues are commonly used in the creation of temporary objects, which can be passed to functions, returned from functions, or used in expressions.
#include <iostream>
#include <string>
std::string createString() {
return "Temporary String"; // The string literal is a prvalue
}
int main() {
std::string str = createString(); // The returned string is a prvalue
std::cout << "str: " << str << std::endl;
return 0;
}
createString()
.createString()
: The temporary std::string
returned by createString()
is a prvalue, which is used to initialize str
.const
ReferencesPrvalues can be bound to const
lvalue references, allowing them to be used as arguments to functions without modification.
#include <iostream>
void print(const int& x) {
std::cout << "Value: " << x << std::endl;
}
int main() {
print(100); // 100 is a prvalue
return 0;
}
100
: The literal 100
is a prvalue, but it can be passed to print()
because print()
takes a const int&
, which can bind to prvalues.const int& x
: This reference binds to the prvalue 100
and allows it to be used in the function.Prvalues play an important role in move semantics, where temporary objects are moved rather than copied, optimizing performance.
#include <iostream>
#include <string>
std::string createString() {
return "Hello, World!";
}
int main() {
std::string str = std::move(createString()); // 'createString()' returns a prvalue
std::cout << "str: " << str << std::endl;
return 0;
}
createString()
: This function returns a prvalue, a temporary std::string
.std::move(createString())
: The prvalue returned by createString()
is cast to an rvalue reference using std::move
, allowing it to be moved into str
.Understanding the relationship between prvalues, lvalues, and xvalues is crucial for writing efficient and correct C++ code.
#include <iostream>
int main() {
int a = 10; // 'a' is an lvalue
int b = a + 20; // 'a + 20' is a prvalue
int&& r = a + 30; // 'a + 30' is a prvalue, but 'r' is an xvalue
std::cout << "r: " << r << std::endl;
return 0;
}
a + 20
): This expression produces a temporary value, which is a prvalue used to initialize b
.r
): r
is an xvalue because it is bound to a temporary value, making it a glvalue.Prvalues are fundamental to C++'s type system and are integral to efficient and expressive programming, particularly in the context of temporary objects and move semantics.
Previous Page | Course Schedule | Course Content