mardi 5 juillet 2016

Is it safe to use unordered_set in gcc 4.8.1? Exception when call unordered_set::find

I have an exception when trying to find an element in unordered_set. It looks like a bug in gcc version 4.8.2. I see that implementation of the set tries to compare the key against the null node:

class Generator {
  unordered_set<unsigned int> generated_keys_;
  Key generateNewKey();
};

Key Generator::generateNewKey() {
  std::mt19937 gen(random_device_());
  std::uniform_int_distribution<> dis(1, numeric_limits<int>::max());

  unsigned int newKey = dis(gen);
  while (generated_keys_.find(newKey) != generated_keys_.end()) { // exception here!
    // if the key was generated before, generate a new ony
    newKey = dis(gen);
  }

  generated_keys_.insert(newKey);
  return Key(newKey);
}

Internally unordered_set::find calls:

bool
_M_equals(const _Key& __k, __hash_code __c, __node_type* __n) const
{
  return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
                 __k, __c, __n);
}

where __n is null. At this point it already has several elements.

Did anyone has the same problem? Is it safe to use gcc 4.8.2?

Aucun commentaire:

Enregistrer un commentaire