2013-05-09 20 views
6

Używam clang (CXX = 'clang ++ -std = C++ 11 -stdlib = libC++') na Mac OS X, z doładowaniem 1.53.0.boost :: uuids :: uuid jako klucz w std :: unordered_map?

Chcę używać UUID jako klucze w unordered_map, ale coraz następujące błędy:

/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 
                ~~~~~~~~~~~~~~~~~^ 

...

Co to jest - to błąd w Boost, co czyni go niezgodnym z moją biblioteką C++? Czy robię coś nie tak? Jakiekolwiek obejścia?

Odpowiedz

12

Dlaczego błąd w dopalaniu? Powinieneś specjalizować szablon std::hash dla 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); 
    } 
}; 

} 

lub po prostu tworzyć unordered_map z boost::hash par

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

lub zapewnić hash funktor, który spełnia wymagania std::hash (dzięki PRAETORIAN).

+2

+1 Zamiast podawać wyraźną specjalizację 'std :: hash', możesz również utworzyć typ (powiedz' uuid_hasher') i zaimplementuj 'uuid_hasher :: operator() (uuid const &)'. Ten typ byłby wówczas trzecim argumentem szablonu dla 'unordered_map' – Praetorian

+0

Myślę, że' operator() 'specjalizacji hash dla twojego typu musi oznaczać' const'. Pozostawienie go bez const nie powiodło się. –

Powiązane problemy