http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51866

             Bug #: 51866
           Summary: [c++0x][4.7 Regression] unordered_set compares
                    moved-out values
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: ro...@binarylife.net


$ cat test.cpp 
#include <unordered_set>
#include <iostream>
#include <cstdlib>

struct num {
  int value;
  num(int n)                 : value(n) {}
  num(num const&)            = default;
  num& operator=(num const&) = default;
  num(num&& o)               : value(o.value) { o.value=-1; }
  num& operator=(num&& o)    { if (this!=&o) { value=o.value; o.value=-1; }
return *this; }
};

struct num_hash {
  size_t operator()(num const& a)const {
    if (a.value<0) std::cout<<"hash of moved value"<<std::endl;
    return 0;
  }
};

struct num_equal {
  bool operator()(num const& a,num const& b)const {
    if (a.value<0 || b.value<0) {
      std::cout<<"compare of moved value: a="<<a.value<<",
b="<<b.value<<std::endl;
      return false;
    }
    return a.value==b.value;
  }
};

int main() {
  std::unordered_multiset<num,num_hash,num_equal> mset;
  mset.insert(num(1));
  mset.insert(num(2));
  mset.insert(num(1));
  mset.insert(num(2));
  if (mset.size()!=4) abort();
  auto iter=mset.cbegin();
  int x0=(iter++)->value;
  int x1=(iter++)->value;
  int x2=(iter++)->value;
  int x3=(iter++)->value;
  if ((x0==1 && x1==1 && x2==2 && x3==2) ||
      (x0==2 && x1==2 && x2==1 && x3==1)) return 0;
  std::cout<<x0<<' '<<x1<<' '<<x2<<' '<<x3<<std::endl;
  abort();
}

$ g++ -std=c++0x test.cpp && ./a.out 
compare of moved value: a=-1, b=1
compare of moved value: a=-1, b=2
compare of moved value: a=-1, b=1
compare of moved value: a=-1, b=1
compare of moved value: a=-1, b=2
compare of moved value: a=-1, b=1
2 1 2 1
Aborted

Reply via email to