我似乎记得这是两个极端之间的折衷,一种是根本不希望隐式生成移动构造函数,另一种是希望在大多数情况下自动生成移动构造函数。
在不希望隐式生成移动构造函数的人中,Dave Abrahams 写了一篇名为Implicit Move Must Go的文章。理由是在某些情况下,即使成员是可移动的,移动构造函数的隐式生成也会破坏不变量。
今年年初(2011 年),委员会决定保留隐式生成移动构造函数,该决定似乎强调了现有代码中的性能提升而不是安全问题(1),Dave 再次发表了关于它的博客。它没有谈论决定的细节,利弊,但对结果也不太满意。
编辑(来自 Jerry Coffin):这是移动构造函数的隐式声明的条件列表:
If the definition of a class X does not explicitly declare a move constructor,
one will be implicitly declared as defaulted if and only if
— X does not have a user-declared copy constructor,
— X does not have a user-declared copy assignment operator,
— X does not have a user-declared move assignment operator,
— X does not have a user-declared destructor, and
— the move constructor would not be implicitly defined as deleted.
基本思想是,在类中包含任何这些都表明隐式生成的移动 ctor 可能行为不端。虽然这是真的,但列表中的条件对于确定来说既不是必要条件也不是充分条件,因此没有生成许多本来有用的移动 ctor,并且可以生成许多会导致问题的移动 ctor。更糟糕的是,这些规则已经足够长和复杂,很少有人记得它们,而修复它们可能至少会加倍。
[杰瑞的贡献/咆哮结束]
(1)感谢 Gene Bushuyev 洞察为何做出该决定