clang error: static_assert failed "Cache the hash code or make functors involved in hash code and bucket index computation default constructible"

Hi,

Consider below test case:

1 #include <unordered_map>

2

3 using key=const int;

4 struct Myhash {

5 Myhash() = default;

6 std::hash hash_int_t;

7 inline size_t operator()(const key& x) const throw() {

8 return hash_int_t(x);

9 }

10 };

11 std::unordered_map<key,int, Myhash> mm;

It compile with gcc (version 4.8.4) whereas I’m unable to compile same with clang.

Clang fails with below errors:

$ clang++ fail.cpp -std=c++11 -O0 –c

In file included from fail.cpp:1:

In file included from /usr/lib/gcc/x86_64-linux-gnu/4.8/…/…/…/…/include/c++/4.8/unordered_map:47:

/usr/lib/gcc/x86_64-linux-gnu/4.8/…/…/…/…/include/c++/4.8/bits/hashtable.h:268:7: error: static_assert failed “Cache the hash code or make functors involved in hash code and bucket index computation default constructible”

static_assert(__if_hash_not_cached<

^ ~~~~~~~~~~~~~~~~~~~~~

/usr/lib/gcc/x86_64-linux-gnu/4.8/…/…/…/…/include/c++/4.8/bits/unordered_map.h:120:36: error: member ‘iterator’ found in multiple base classes of different types

typedef typename _Hashtable::iterator iterator;

^

In file included from fail.cpp:1:

In file included from /usr/lib/gcc/x86_64-linux-gnu/4.8/…/…/…/…/include/c++/4.8/unordered_map:48:

/usr/lib/gcc/x86_64-linux-gnu/4.8/…/…/…/…/include/c++/4.8/bits/unordered_map.h:121:36: error: member ‘const_iterator’ found in multiple base classes of different types

typedef typename _Hashtable::const_iterator const_iterator;

In file included from fail.cpp:1:

In file included from /usr/lib/gcc/x86_64-linux-gnu/4.8/…/…/…/…/include/c++/4.8/unordered_map:48:

/usr/lib/gcc/x86_64-linux-gnu/4.8/…/…/…/…/include/c++/4.8/bits/unordered_map.h:124:36: error: member ‘size_type’ found in multiple base classes of different types

typedef typename _Hashtable::size_type size_type;

^

In file included from fail.cpp:1:

In file included from /usr/lib/gcc/x86_64-linux-gnu/4.8/…/…/…/…/include/c++/4.8/unordered_map:48:

/usr/lib/gcc/x86_64-linux-gnu/4.8/…/…/…/…/include/c++/4.8/bits/unordered_map.h:452:7: error: multiple overloads of ‘erase’ instantiate to the same signature ‘iterator (iterator)’ (aka ‘int (int)’)

erase(iterator __it)

^

Is this a known issue, or something I missed here ?

Regards,

Ashutosh

Is it related to this libstdc++ bug?

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

Does your libstdc++’s hashtable_policy.h have this change?

https://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/include/bits/hashtable_policy.h?r1=195935&r2=195934&pathrev=195935

FWIW this test case works on clang 3.6 and 3.9.0 with the libstdc++ at Godbolt Compiler Explorer

-Brian

Thanks Brian for pointing out.

Yes my libstdc++ doesn’t have mentioned fix, after applying it worked !.

Regards,

Ashutosh