Why does this need an explicit std::move?

后端 未结 4 836
失恋的感觉
失恋的感觉 2020-12-18 20:57

Let\'s say I got a Foo class containing an std::vector constructed from std::unique_ptr objects of another class, Bar.

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-18 21:43

    bar is actually an lvalue, so you need to pass it through std::move, so that it is seen as an rvalue in the call to push_back.

    The Foo::AddBar(UniqueBar&& bar) overload simply ensures that this overload is picked when an rvalue is passed in a call to Foo::AddBar. But the bar argument itself has a name and is an lvalue.

提交回复
热议问题