赋值(运算符 =)使容器的迭代器失效

Assignment (operator =) invalidates iterators for containers

本文关键字:迭代器 失效 运算符 赋值      更新时间:2023-10-16

我有一个这样的代码:

std::vector<int> v1 = { 1, 2, 3, 4 };  
std::vector<int> v2 = { 7, 8, 9, 10 };  
std::vector<int>::iterator it = std::next(v1.begin());  
v1 = v2;  
int test = *it;  
std::cout << test;   

上面的代码将抛出一个错误:迭代器不可取消引用。

但是,如果我用列表替换向量,如下所示:

std::list<int> v1 = { 1, 2, 3, 4 };  
std::list<int> v2 = { 7, 8, 9, 10 };  
std::list<int>::iterator it = std::next(v1.begin());  
v1 = v2;  
int test = *it;  
std::cout << test;   

代码只是按预期运行,没有错误。
从迭代器失效规则和 std::list::operator= 中,我被告知在调用运算符 = 后,与此容器相关的所有迭代器、引用和指针都将失效,除了结束迭代器。但是为什么上面带有 std::list 的代码有效?我是否误解了某些重要的东西?

当迭代器失效时,取消引用它是未定义的行为。因此,就规范而言,您在这样做时观察到的任何行为都是无关紧要的。根据您的期望,"工作"是允许的行为之一。

FWIW(不多,tbh),我希望实现std::list赋值运算符等效于这样的东西:

list& operator=(list const& rhs) {
    if (this == &rhs)
        return *this;
    auto lhs_i = begin();
    auto rhs_i = rhs.begin();
    // write over the elements in any currently existing nodes, this
    // avoids any unnecessary allocations
    while (lhs_i != end() && rhs_i != rhs.end()) {
        *lhs_i++ = *rhs_i++;
    }
    // erase any extra elements if size() > rhs.size()
    erase(lhs_i, end());
    // push back additional elements if size() < rhs.size()
    while (rhs_i != rhs.end()) {
        push_back(*rhs_i++);
    }
    return *this;
}

如果是这样,您可以看到,对于像您这样的情况,列表都具有相同数量的元素,不会创建或销毁任何元素,因此您非常希望迭代器能够完全正常工作。当然,这完全是猜测,绝对不是您应该依赖的行为,因为即使您的实现是这种情况,他们也可以在下一个版本中更改它,恕不另行通知。

这是未定义的行为,但 GCC 具有调试容器来捕获这种行为。

使用以下-D_GLIBCXX_DEBUG启用它:

     int test = *it;
         ^~~~
/usr/local/include/c++/6.1.0/debug/safe_iterator.h:270:
Error: attempt to dereference a singular iterator.
Objects involved in the operation:
    iterator "this" @ 0x0x7fff5f561e90 {
      type = __gnu_debug::_Safe_iterator<std::__cxx1998::_List_iterator<int>, std::__debug::list<int, std::allocator<int> > > (mutable iterator);
      state = singular;
      references sequence with type 'std::__debug::list<int, std::allocator<int> >' @ 0x0x7fff5f561ef0
    }
bash: line 7: 16071 Aborted                 (core dumped) ./a.out