Are value parameters implicitly moved when returned by value?

谁都会走 提交于 2019-11-27 21:34:23

问题


Consider the following function:

Foo foo(Foo x)
{
    return x;
}

Will return x invoke the copy constructor or the move constructor? (Let's leave NRVO aside here.)

To investigate, I wrote a simple Foo class that is only movable but not copyable:

struct Foo
{
    Foo() = default;
    Foo(const Foo&) = delete;
    Foo(Foo&&) = default;
};

If the move constructor were invoked when returning value parameters by value, all should be fine. But the current g++ compiler complains about return x with the following error message:

error: deleted function 'Foo::Foo(const Foo&)'

If I replace return x with return std::move(x), everything is fine. From this I conclude that moving from value parameters must be done explicitly if desired. Is g++'s behavior conforming or not?


回答1:


If there is a move ctor for Foo, it should be selected.

Function parameters are explicitly excluded from copy elision in return statements (FDIS §12.9p31, first bullet):

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

However, the next paragraph explicitly brings move ctors back into consideration:

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

(Emphasis is mine in both quotes.)




回答2:


This is valid code- G++'s behaviour is non-conformant. MSVC10 does support this behaviour.



来源:https://stackoverflow.com/questions/6009004/are-value-parameters-implicitly-moved-when-returned-by-value

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