Move constructor suppressed by comma operator

て烟熏妆下的殇ゞ 提交于 2019-12-01 15:14:31

Automatic move is based on eligibility for copy elision:

§12.8 [class.copy] p32

When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. [...]

And copy elision in turn is allowed when the return expressions is the name of an automatic object.

§12.8 [class.copy] p31

in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

With the comma operator inserted, the expression is not the name of an automatic object anymore, but only a reference to one, which suppresses copy elision.

t is a local, named variable, and thus an lvalue. The comma operator behaves as documented.

Rather, you should be asking why return t; allows t to bind to an rvalue reference - that's the real magic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!