14

I'm using clang (CXX='clang++ -std=c++11 -stdlib=libc++') on Mac OS X, with boost 1.53.0.

I want to use uuid as keys in unordered_map, but getting the following errors:

/usr/bin/../lib/c++/v1/type_traits:748:38: error: implicit instantiation of undefined template
      'std::__1::hash<boost::uuids::uuid>'
    : public integral_constant<bool, __is_empty(_Tp)> {};
                                 ^
/usr/bin/../lib/c++/v1/unordered_map:327:54: note: in instantiation of template class
      'std::__1::is_empty<std::__1::hash<boost::uuids::uuid> >' requested here
template <class _Key, class _Tp, class _Hash, bool = is_empty<_Hash>::value

...

/usr/bin/../lib/c++/v1/unordered_map:327:71: error: no member named 'value' in
      'std::__1::is_empty<std::__1::hash<boost::uuids::uuid> >'
template <class _Key, class _Tp, class _Hash, bool = is_empty<_Hash>::value
                                                     ~~~~~~~~~~~~~~~~~^

...

What is it - a bug in Boost, which makes it incompatible with my C++ lib? Or I am doing something wrong? Any workarounds?

stardust
  • 5,918
  • 1
  • 18
  • 20
rincewind
  • 1,103
  • 8
  • 29

1 Answers1

23

Why bug in boost? You should specialize std::hash template for boost::uuid.

#include <boost/functional/hash.hpp>

namespace std
{

template<>
struct hash<boost::uuids::uuid>
{
    size_t operator () (const boost::uuids::uuid& uid)
    {
        return boost::hash<boost::uuids::uuid>()(uid);
    }
};

}

or, simply create unordered_map with boost::hash par

std::unordered_map<boost::uuids::uuid, T, boost::hash<boost::uuids::uuid>>

or provide hash functor that satisfies requirements of std::hash (thanks to Praetorian).

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • 3
    +1 Instead of providing an explicit specialization of `std::hash` you could also create a type (say `uuid_hasher`) and implement `uuid_hasher::operator()(uuid const&)`. That type would then be the third template argument for the `unordered_map` – Praetorian May 09 '13 at 21:11
  • 1
    I think `operator()` of hash's specialization for your type needs to marked `const`. Leaving it non-const failed to compile. – The Vivandiere Sep 02 '16 at 13:36