有一个复制构造函数:
Implementation(const Implementation & other) :
Implementation(other.clone())
{}
还有一个功能clone():
Implementation Implementation::clone() const
{
return Implementation(this->number(), this->string(), this->vector());
}
移动构造函数:
Implementation( Implementation && other) noexcept :
Implementation()
{
this->swap(other);
}
我不明白为什么不在线调用Implementation(other.clone())移动构造函数(定义了移动构造函数)。毕竟,该函数clone()只返回调用它的对象的副本。什么叫做?
编译器完全有权使用复制省略,也就是说,在进一步赋值的地方用对象的构造来替换移动。
为了证实我的话,我使用了在线编译器:
资源:
我们感兴趣的函数在 Intel x86_64 汇编语言中的编译结果:
如您所见,编译器实际上应用了我指出的技术。指针
this通过寄存器传递(E/R)CX。也就是说,Implementation::Implementation(Implementation const&)在收到this时(在构造函数的情况下,它指向对象的未初始化内存区域),并进一步传递它,clone()不做任何修改。