xvalues,glvalues和prvalues的现实生活的例子? [英] Real life examples of xvalues, glvalues, and prvalues?

查看:97
本文介绍了xvalues,glvalues和prvalues的现实生活的例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以告诉或解释一些现实生活中的xvalues,glvalues和prvalues的例子。我已读过类似的问题:

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

什么是右值,左值,x值,glvalues和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?

推荐答案

考虑下面这个类:

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

表达式 some_foo.copy_of_name() std :: string prvalue $ c>),而不是引用。每一个贬值也是一个右值。 (R值更一般。)

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.)

表达式 some_foo.original_name() strong>,因为 original_name 返回一个左值引用 std :: string& )。每一个左值也是一个glvalue。 (Glvalues更常见。)

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.)

表达式 std :: move(some_name)是一个 xvalue ( std :: string&& ),因为 std :: move code>)。

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;

鉴于上述声明,表达式 a b c 都是左值。

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

这篇关于xvalues,glvalues和prvalues的现实生活的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆