繁体   English   中英

为什么std :: vector有两个赋值运算符?

[英]Why does std::vector have two assignment operators?

自2011年以来,我们同时拥有复制和移动任务。 但是, 这个答案非常有说服力地说,对于资源管理类,只需要一个赋值运算符。 例如,对于std::vector ,这看起来像

vector& vector::operator=(vector other)
{
  swap(other);
  return*this;
}

这里重要的一点是,论证是以价值为基础的。 这意味着在输入函数体的时刻,大部分工作已经由other构造完成(如果可能的话,通过移动构造函数,否则通过复制构造函数)。 因此,这会自动正确地实现复制和移动分配。

如果这是正确的,为什么(根据这个文档至少std::vector 没有以这种方式实现?


编辑以解释这是如何工作的。 考虑以下示例中上述代码中的other内容会发生什么

void foo(std::vector<bar> &&x)
{
  auto y=x;             // other is copy constructed
  auto z=std::move(x);  // other is move constructed, no copy is ever made.
  // ...
}

如果元素类型不是可复制的,或者容器不遵守强异常保证,则在目标对象具有足够容量的情况下,复制赋值运算符可以避免分配:

vector& operator=(vector const& src)
{
    clear();
    reserve(src.size());  // no allocation if capacity() >= src.size()
    uninitialized_copy_n(src.data(), src.size(), dst.data());
    m_size = src.size();
}

实际上定义了三个赋值运算符:

vector& operator=( const vector& other );
vector& operator=( vector&& other );
vector& operator=( std::initializer_list<T> ilist );

您的建议vector& vector::operator=(vector other)使用复制和交换习惯用法。 这意味着,当调用操作符时,原始矢量将被复制到参数中,复制矢量中的每个项目。 然后这个副本将与this交换。 编译器可能能够忽略该副本,但该副本省略是可选的,移动语义是标准的。

您可以使用该习惯用法替换复制赋值运算符:

vector& operator=( const vector& other ) {
    swap(vector{other}); // create temporary copy and swap
    return *this;
}

每当复制任何元素抛出时,此函数也会抛出。

要实现移动赋值运算符,请忽略复制:

vector& operator=( vector&& other ) {
    swap(other);
    return *this;
}

由于swap()永远不会抛出,因此移动赋值运算符也不会抛出。

通过使用移动赋值运算符和匿名临时initializer_list也可以轻松实现initializer_list -assignment:

vector& operator=( std::initializer_list<T> ilist ) {
    return *this = vector{ilist};
}

我们使用了移动赋值运算符。 作为一个概念, initializer_list赋值operatpr只会在其中一个元素实例化时抛出。

正如我所说,编译器可能能够删除副本以进行复制分配。 但编译器没有义务实现该优化。 它有义务实现移动语义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM