https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94805

            Bug ID: 94805
           Summary: variant hash algorithm is collision-prone
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: charlie at charliedyson dot net
  Target Milestone: ---

The hash implementation for std::variant appears to be "index +
hash(contained_value)". This leads to collisions when working with numeric
types (e.g. a variant of various enums). Ideally a standard hash-combining
operation would be used instead of "+".

Example:

#include <variant>
#include <iostream>

enum E {ZERO, ONE};

int main ()
{
    using V = std::variant<E, int>;
    V v_enum = ONE;
    V v_int = 0;
    std::cout << std::hash<V> () (v_enum) << std::endl;
    std::cout << std::hash<V> () (v_int) << std::endl;

}

Output is

1
1

Reply via email to