什么表达式创建xvalues? [英] What expressions create xvalues?

查看:114
本文介绍了什么表达式创建xvalues?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解C ++ 11的概念。



我说的标准草案:


xvalue(eXpiring值)也指一个对象,通常接近其生命周期的末尾(例如,它的
资源可能被移动)。 xvalue是某些种类的涉及
右值引用(8.3.2)的表达式的结果。 [示例:调用返回类型为右值
引用的函数的结果是xvalue。 -end example]


确定,因此产生xvalues的某些种类的表达式?



我理解lvalue和prvalue(至少我想,我明白了)。

解决方案

在§5(C ++ 11§5[expr] / 6)的引言中有一个有用的非规范性注释:


[注意:表达式是xvalue,如果它是:




  • 调用函数的结果(无论是隐式还是显式),其返回类型是对对象类型的右值引用。


  • 对象类型的右值引用,


  • 类成员访问表达式指定非引用类型的非静态数据成员,其中对象表达式是x值,或


  • a 。* 指针成员表达式,其中第一个操作数是xvalue第二个操作数是指向数据成员的指针。




通常,此规则的效果是名为rvalue引用被视为左值,对对象的未命名右值引用被视为x值;函数的右值引用被视为无论是否命名的左值。 - end note]


通过搜索§5的其余部分,此列表显示为详尽无遗。该列表后面是一个示例:


  struct A {
int m;
};

A&&运算符+(A,A);
A&& F();
A a;
A&& ar = static_cast< A&&>(a);

表达式 f() f()。m static_cast< A&&>(a) a + a 是xvalues。 ar 是一个左值。


有两种常见的方法xvalue表达式:




  • 使用 std :: move 目的。 std :: move 对右值引用类型执行 static_cast ,并返回右值引用。

    li>
  • 使用 std :: forward 转发右值。 std :: forward 通常用于函数模板中,以便完美转发函数参数。



    提供给函数模板的参数是一个右值,参数类型将是一个右值引用,它是一个左值。在这种情况下, std :: forward 对右值引用类型执行 static_cast ,并返回右值引用。 p>

    (注意:如果提供给函数模板的参数是一个左值,则参数类型将是一个左值引用, std :: forward 将返回一个左值引用。)



I'm trying to understand the C++11 concepts.

The standard draft which I have says:

An xvalue (an "eXpiring" value) also refers to an object, usually near the end of its lifetime (so that its resources may be moved, for example). An xvalue is the result of certain kinds of expressions involving rvalue references (8.3.2). [ Example: The result of calling a function whose return type is an rvalue reference is an xvalue. —end example ]

OK, so what exactly are the "certain kinds of expressions" that produce xvalues? This part of the spec does not detail a list of these expressions.

I understand lvalue and prvalue (at least I think, I understand).

解决方案

There is a helpful non-normative note in the introduction to §5 (C++11 §5[expr]/6):

[ Note: An expression is an xvalue if it is:

  • the result of calling a function, whether implicitly or explicitly, whose return type is an rvalue reference to object type,

  • a cast to an rvalue reference to object type,

  • a class member access expression designating a non-static data member of non-reference type in which the object expression is an xvalue, or

  • a .* pointer-to-member expression in which the first operand is an xvalue and the second operand is a pointer to data member.

In general, the effect of this rule is that named rvalue references are treated as lvalues and unnamed rvalue references to objects are treated as xvalues; rvalue references to functions are treated as lvalues whether named or not. —end note ]

Searching through the rest of §5, this list appears exhaustive. The list is followed by an example:

struct A {
    int m;
};

A&& operator+(A, A);
A&& f();
A a;
A&& ar = static_cast<A&&>(a);

The expressions f(), f().m, static_cast<A&&>(a), and a + a are xvalues. The expression ar is an lvalue.

There are two common ways to get an xvalue expression:

  • Use std::move to move an object. std::move performs a static_cast to an rvalue reference type and returns the rvalue reference.

  • Use std::forward to forward an rvalue. std::forward is typically used in a function template to enable perfect forwarding of a function argument.

    If the argument provided to the function template was an rvalue, the parameter type will be an rvalue reference, which is an lvalue. In this case, std::forward performs a static_cast to an rvalue reference type and returns the rvalue reference.

    (Note: If the argument provided to the function template was an lvalue, the parameter type will be an lvalue reference and std::forward will return an lvalue reference.)

这篇关于什么表达式创建xvalues?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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