The Move-only type returned by the transform constructor

The following code returns the type of move, which then needs to be converted to another type using the conversion constructor.

#include <utility>

class Foo
{
public:
  Foo() {}
  Foo(const Foo&) = delete;
  Foo(Foo&&) = default;
};

class Other
{
public:
  Other(Foo foo) {}
};


Other moo()
{
  Foo foo;
  return foo;
}

int main()
{
  moo();
}

This generated a bug with my compiler and can be fixed by adding std::movea return statement, which is considered bad practice, because in general it prevents the return value from being optimized. Should the identifier of the returned statement be first processed as an rvalue to satisfy the transforms?

Is this code valid and which compiler is here here?

+4
2

, return rvalue , ?

. [class.copy] CWG 1579 ( ++ 17, ++ 14. , , ...):

- :

  • return (, ) id-, , -- -,
  • [...]

, rvalue. , , rvalue (, cv), , lvalue.

, , foo rvalue. Other(Foo ) Foo(Foo&& ).

Other(Foo ) rvalue, , foo lvalue, . , clang . Other(Foo&& ), clang .

+4

, :

​​ std::move return, , .

. NRVO , RVO . .

+2

Source: https://habr.com/ru/post/1685231/


All Articles