Should I move the return value of the called party if I am going to build an object with it?

class Item { };

class Wrapper
{
    Item i;
    Wrapper(const Item& mI) : i{mI} { }
    Wrapper(Item&& mI) : i{std::move(mI)} { }
};

Item createItem()
{
    Item result;
    // ...
    return result; // No `std::move` needed here
}

Wrapper createWrapper()
{
    Wrapper result{createItem()}; // `std::move(createItem())` ?
    return result;
}

createItem()works efficiently without a call std::move.

createWrapper()uses the return value createItem()and wants to call the constructor Wrapper::Wrapper(Item&&).

Should I use Wrapper result{std::move(createItem())};or will it be Wrapper result{createItem()};enough?

+4
source share
2 answers

There is no need std::move. The value of the function call expression is already rvalue.

+8
source

std::movenot required to return objects:

BigMatrix BuildBigMatrix()
{
   ...
   return someBigMatrix; // *moved* outside of function
}
0
source

All Articles