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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…