是临时对象xvalue吗? [英] Are temporary objects xvalues?

查看:84
本文介绍了是临时对象xvalue吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写学位论文,其中还涉及对C ++ 11背后理论的一些解释,这很好,因为C ++是我选择的编程语言,并且该标准或多或少是免费的(N3337)

I am currently writing my degree dissertation and it involves also some explaining of the theory behind C++11 which is really fine as C++ is my programming language of choice and the standard is more or less available for free (N3337) to get yourself lost in.

但是我在尝试准确而详细地解释新的xvalue类别时碰壁了。 据我了解,临时对象始终是xvalue,但我在标准中找不到该引用。据我了解,函数表达式的值类别调用了一个函数,具有非引用返回类型,是xvalue。该标准说: xvalue是某些包含rvalue-references的表达式的结果,这使我很烦。例如:

Yet I have hit a wall while trying to explain the new xvalue category accurately and in detail. It is my understanding that a temporary object is always a xvalue but I cannot find any reference to this in the standard. It is my understanding that the value category of an expression of a function call to a function that has a non-reference return type, is a xvalue. The standard says that "a xvalue is the result of certain kind of expressions that involve rvalue-references" which is bugging me. For example:

TestClass { ... };
testClass createObject() { return testClass(); }

void someFunction(TestClass& testClass) { ... }
void someFunction(TestClass&& testClass) { ... }

someFunction(createObject());

上面将按预期方式调用以rvalue-reference为参数的重载函数。但是createObject()不返回右值引用,它返回类型为TestClass的临时对象。现在的问题是,我必须解释其背后的原因。表达式 createObject()的计算结果是什么?如果确实是一个xvalue,因为它返回了一个临时对象,则其背后的原因很明确,并且在重载解析期间倾向于使用rvalue-reference。如果不是,那么对标准的这种行为有何解释?有没有定义一些隐式转换逻辑,而我还没有找到?

The above will, as expected, call the overloaded function which takes an rvalue-reference as parameter. Yet createObject() does not return a rvalue-reference, it returns a temporary object of type TestClass. My problem is now, I have to explain the reasons behind it. What does the expression "createObject()" evaluate to? If it is indeed a xvalue because it returns a temporary object, the reasoning behind it is clear and the rvalue-reference is favored during overload resolution. If it does not, what is the explanation for this behaviour with regard to the standard? Is there some implicit conversion logic defined somewhere that I haven't found yet?

如果有人能帮助我解决这一问题,我将非常感谢挖掘和阅读,我还没有想出一个合理的解释。

I'd really appreciate if someone could help me out on this one because even after days of digging and reading, I have yet to come up with a sound explanation. Thanks a lot in advance.

推荐答案

对象从不为{l | r | x}值。值类别描述表达式

Objects are never {l|r|x}values. value categories describe expressions.

xvalue 是函数调用表达式的值类别,其中函数返回类型为对对象的右值引用(例如 std :: move ),它也是强制转换表达式的值类别,其中强制转换是对对象的右值引用(例如 std :: move )的内胆。

xvalue is the value category of the function call expression where the function return type is an rvalue reference to object (e.g. std::move), and it is also the value category of the cast expression where the cast is to an rvalue reference to object (e.g. the guts of std::move).

函数调用表达式 createObject()<在您的示例中/ code>是 prvalue 表达式,因为它是对具有非引用返回类型的函数的函数调用。

The function call expression createObject() in your example is an prvalue expression because it is a function call to a function with non-reference return type.

这篇关于是临时对象xvalue吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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