将从函数返回的Rvalue分配给另一个Rvalue [英] Assigning Rvalue returned from function to another Rvalue

查看:117
本文介绍了将从函数返回的Rvalue分配给另一个Rvalue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类测试{

public:

int n1;

};

测试func(){

return Test();

}

int main(){

func()= Test();

}

这对我来说没有意义。如何以及为什么允许这样做?它是未定义的行为吗?如果一个函数返回一个右值,那么如何将一个右值设置为另一个右值?如果我尝试使用任何原始类型进行此操作,都会给我一个我所期望的错误。



我知道左值在内存中的位置,因此函数创建一个临时左值(rvalue?)并将其分配给另一个左值?有人可以解释这种语法的工作原理吗?

解决方案

函数调用表达式的值类别实际上是一个右值。



确实,您不能在右值基元上调用复制赋值运算符。实际上,C语言中右值的历史定义是区别在于它们可能不在集合的左侧。



类的赋值运算符有些不同,虽然。它们是常规的成员函数。而且没有规则可以阻止调用右值的成员函数。的确,当函数具有副作用时,它通常非常有用。手势,改变临时状态。声明之后,临时对象将被丢弃。没有UB,只有无意义的复制。这: Test&运算符=(测试)& =默认值; 。引用限定符仅在c ++ 11中稍后才添加,因此(隐式)副本分配不能指定为较早地具有引用限定符。据推测,C ++ 11并未更改隐式副本构造函数的限定符,以防止破坏确实分配给右值的旧代码,即使这种分配似乎毫无意义。 高完整性C ++编码Standard 建议您对用户定义的副本分配运算符使用ref限定符。


class Test {

public:

    int n1;

};

Test func() {

    return Test();

}

int main() {

    func() = Test();

}

This doesn't make sense to me. How and why is this allowed? Is it undefined behavior? If a function returns an rvalue, then how is it possible to set an rvalue to another rvalue? If I tried this with any primitive types, it would give me an error like I expect.

I know that lvalues are a place in memory, so is the function creating a temporary lvalue (rvalue?) and assigning it to another lvalue? Can someone explain how this syntax works?

解决方案

The value category of the function call expression is in fact an rvalue.

Indeed, you may not call the copy assignment operator on rvalue primitives. The historical definition of rvalues in C was in fact the distinction that they may not be on the left side of an assigment.

The assignment operators of classes are a bit different, though. They're regular member functions. And no rule prevents calling member functions of rvalues. Indeed, it's often quite useful when the functions have side-effects.

How it works, well, the copy assignment operator is called on the temporary, the operator copies the right hand argument, changing the state of the temporary. After the statement, the temporary object is discarded. There is no UB, just pointless copying.

You can prevent calling copy assignment on an rvalue, by declaring the operator with a reference qualifier like this: Test& operator=(Test) & = default;. Ref-qualifiers were added only later in c++11, so (implicit) copy assignment couldn't have been specified to be ref-qualified earlier. Presumably, C++11 didn't change the qualifier of implicit copy constructor to prevent breaking old code that does assign to an rvalue, even though such assignment does seem quite pointless. The High Integrity C++ Coding Standard recommends that you do use ref qualifier with user defined copy assignment operators.

这篇关于将从函数返回的Rvalue分配给另一个Rvalue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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