Move constructor and initialization list

2019-03-29 08:37发布

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条回答
Juvenile、少年°
2楼-- · 2019-03-29 09:03

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.

查看更多
登录 后发表回答