2012-12-10 55 views
6

我想为某个类型实现移动构造函数(无拷贝构造函数),该构造函数需要是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 
} 

我想写这使boost::unordered_map< Key , Composite >不需要拷贝构造函数,并且只使用移动的构造。如果可能,我不想在Composite的移动构造函数的初始化列表中使用Base的拷贝构造函数。

这可能吗?

回答

11

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

作为金科玉律,只要你通过右值引用拿东西,你需要使用它里面std::move,每当你需要通过通用参考(即推断模板类型与&&)的东西,你需要使用它里面std::forward

+1

+1为了更好地使用相对较新的术语* universal reference *。 – mavam

+1

@MatthiasVallentin:这个词是由Scott Meyers当场作出的。标准委员会现在更喜欢“转发参考”这个术语。 –

+0

现在我们差不多两年了,很高兴看到社区已经在一个稳定的时期内融合了。 – mavam