问题描述
C ++编译器自动生成复制构造函数和复制赋值运算符。为什么不 swap
也?
C++ compilers automatically generate copy constructors and copy-assignment operators. Why not swap
too?
这些天,实现复制赋值运算符的首选方法是复制-swap idiom:
These days the preferred method for implementing the copy-assignment operator is the copy-and-swap idiom:
T& operator=(const T& other)
{
T copy(other);
swap(copy);
return *this;
}
( 。根据(我不知道如何读得很好),它是回迁。它在标题为未准备好C ++ 0x,但打开以后将重新提交的部分列出。所以看起来像它不会是C ++ 0x的一部分,但可能稍后添加。
I did a bit of searching, and the proposal for = default
move was proposal n2583
. According to this (which I don't know how to read very well), it was "moved back." It is listed under the section titled "Not ready for C++0x, but open to resubmit in future ". So looks like it won't be part of C++0x, but may be added later.
有点失望。 :(
编辑2:再看一下,我发现这个:这是更新的,看起来像我们可以默认 move
。Yay!
EDIT 2: Looking around a bit more, I found this: Defining Move Special Member Functions which is much more recent, and does look like we can default move
. Yay!
这篇关于为什么在C ++ 0x中没有编译器生成的swap()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!