简体   繁体   English

GCC 4.9的unordered_set和std :: move

[英]GCC 4.9's unordered_set and std::move

When moving an unordered_set out on GCC 4.9, and then reusing the moved-from object, I am getting a divide-by-zero when I add to it. 当在GCC 4.9上移动unordered_set ,然后重新使用移动的对象时,我在添加它时会得到除零。

My understanding (from http://en.cppreference.com/w/cpp/utility/move ) is that the moved-from object can be used provided none of its preconditions are violated. 我的理解(来自http://en.cppreference.com/w/cpp/utility/move )是可以使用移动的对象,前提是它没有违反任何先决条件。 Calling clear() on the moved-from set is fine (which makes sense in the context of preconditions), but it's not clear to me that I'm violating any precondition by adding a new element. 在移动的集合上调用clear()很好(这在前置条件的上下文中是有意义的),但是我不清楚我是否通过添加新元素来违反任何前提条件。

Example code: 示例代码:

#include <unordered_set>
using namespace std;
void foo(unordered_set<int> &&a) {
  unordered_set<int> copy = std::move(a);
}

void test() {
  unordered_set<int> a;
  for (int i = 0; i < 12; ++i) a.insert(i);
  foo(std::move(a));

  a.clear();
  a.insert(34); // divide by zero here
}

int main() {
  test();
}​

This code works fine on GCC4.7 - is this an issue in GCC4.9's unordered_set implementation, or in my understanding of what it means to violate preconditions on moved-from objects? 这段代码在GCC4.7上运行正常 - 这是GCC4.9的unordered_set实现中的问题,还是我理解违反移动对象的前提条件意味着什么?

This is PR 61143 . 这是PR 61143 It has been fixed for gcc-4.10 and the fix has been backported in time for 4.9.1. 它已针对gcc-4.10进行了修复,修复程序已及时向后移植4.9.1。

Marc Glisse has already referred you to the GCC bug, but to answer your question: Marc Glisse已经向您介绍了GCC错误,但回答了您的问题:

is this an issue ... in my understanding of what it means to violate preconditions on moved-from objects? 这是一个问题......在我理解违反移动对象的前提条件意味着什么?

No, it isn't, your understanding is correct. 不,不是,你的理解是正确的。 Making the code in your question work isn't just a GCC extension, your program is perfectly valid. 使问题中的代码工作不仅仅是GCC扩展,您的程序完全有效。 A moved-from object of a type defined in the standard library is supposed to remain usable. 标准库中定义的类型的移动对象应该保持可用。 You don't know anything about its state, but, as an example, any container is empty, or is non-empty, so a moved-from container is also either empty or non-empty. 您对其状态一无所知,但是,作为示例,任何容器都是空的,或者是非空的,因此移动的容器也可以是空的也可以是非空的。 Which of those is unspecified, but your program can check, and the standard library implementation must behave as appropriate for the result of your program's check. 其中哪些是未指定的,但您的程序可以检查,标准库实现必须适合您的程序检查结果。

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

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