4

Is it legal to return by value an object with a deleted copy constructor? For example, consider an object with an std::unique_ptr member. Most compilers do not complain when returning such objects by value because in most cases the compiler won't even look for the copy constructor. However, since (N)RVO is not required by the standard, is it okay to say that such programs are legal? Is std::move in the return statement necessary for standard compliance in these cases?

1 Answers1

5

If you have a working move constructor, you may delete the copy constructor.

The following program works for me.

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

Foo test()
{
   Foo f;
   return f;
}

int main()
{
   Foo f = test();
   return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270