Move constructor and initialization list

强颜欢笑 提交于 2019-12-09 11:57:49

问题


I want to implement move constructors (no copy constructor) for a certain type that needs to be a value type in a boost::unordered_map. Let's call this type Composite.

Composite has the following signature:

struct Base
{
  Base(..stuff, no default ctor) : initialization list {}
  Base(Base&& other) : initialization list {} 
}

struct Composite
{
  Base member;
  Composite(..stuff, no default ctor) : member(...) {}
  Composite(Composite&& other) : member(other.member) {} // <---- I want to make sure this invokes the move ctor of Base
}

I want to write this so boost::unordered_map< Key , Composite > does not require the copy constructor, and just uses the move constructor. If possible, I don't want to use the copy constructor of Base in the initialization list of move constructor of Composite.

Is this possible?


回答1:


Say member(std::move(other.member)).

As a golden rule, whenever you take something by rvalue reference, you need to use it inside std::move, and whenever you take something by universal reference (i.e. deduced templated type with &&), you need to use it inside std::forward.



来源:https://stackoverflow.com/questions/13793343/move-constructor-and-initialization-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!