移动构造函数和初始化列表

lur*_*her 6 c++ unordered-map initialization move-semantics c++11

我想为某个类型实现移动构造函数(没有复制构造函数),该类型需要是一个值类型boost::unordered_map.我们称之为这种类型Composite.

Composite 有以下签名:

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
}
Run Code Online (Sandbox Code Playgroud)

我想写这个,所以boost::unordered_map< Key , Composite >不需要复制构造函数,只需使用移动构造函数.如果可能的话,我不想Base在移动构造函数的初始化列表中使用复制构造函数Composite.

这可能吗?

Ker*_* SB 11

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

作为一个黄金法则,每当你通过右值引用来获取某些内容时,你需要在内部使用它std::move,并且每当你通过通用引用(即推导出的模板类型&&)获取某些东西时,你需要在里面使用它std::forward.