Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I was wondering if anyone could tell or explain some real life examples of xvalues, glvalues, and prvalues?. I have read a similar question :

What are rvalues, lvalues, xvalues, glvalues, and prvalues?

but I did not understand what everyone meant. Can anyone explain in what cases these values are important and when one should use them?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
305 views
Welcome To Ask or Share your Answers For Others

1 Answer

Consider the following class:

class Foo
{
    std::string name;

public:

    Foo(std::string some_name) : name(std::move(some_name))
    {
    }

    std::string& original_name()
    {
        return name;
    }

    std::string copy_of_name() const
    {
        return name;
    }
};

The expression some_foo.copy_of_name() is a prvalue, because copy_of_name returns an object (std::string), not a reference. Every prvalue is also an rvalue. (Rvalues are more general.)

The expression some_foo.original_name() is an lvalue, because original_name returns an lvalue reference (std::string&). Every lvalue is also a glvalue. (Glvalues are more general.)

The expression std::move(some_name) is an xvalue, because std::move returns an rvalue reference (std::string&&). Every xvalue is also both a glvalue and an rvalue.


Note that names for objects and references are always lvalues:

std::string a;
std::string& b;
std::string&& c;

Given the above declarations, the expressions a, b and c are lvalues.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...