prvalue


Concept: Prvalue

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.

Key Characteristics of Prvalues

Breakdown of Value Categories

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.

Example 1: Simple Prvalues

#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;
}

Explanation:

Example 2: Prvalues in Function Return Values

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;
}

Explanation:

Example 3: Prvalues and Temporary Objects

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;
}

Explanation:

Example 4: Binding Prvalues to const References

Prvalues 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;
}

Explanation:

Example 5: Prvalues and Move Semantics

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;
}

Explanation:

Prvalues and Value Categories

Understanding the relationship between prvalues, lvalues, and xvalues is crucial for writing efficient and correct C++ code.

Prvalues:

Lvalues:

Xvalues:

Example 6: Prvalues vs. Glvalues

#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;
}

Explanation:

Summary

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