2016-07-29 55 views
0

當我聲明一個std::unordered_map<boost::any, boost::any>類型的變量時,它會引發惱人的編譯錯誤。std :: unordered_map <boost :: any,boost :: any>拋出惱人的編譯錯誤

有關示例,any.cc

#include <map> 
#include <boost/any.hpp> 

int main() { 
    std::map<boost::any, boost::any> dict; 
    return 0; 
} 

編譯上述代碼作爲g++ any.cc -std=c++11 -I/usr/include/boost發生很多錯誤像以下:

In file included from /usr/include/c++/5/bits/hashtable.h:35:0, 
       from /usr/include/c++/5/unordered_map:47, 
       from any.cc:1: 
/usr/include/c++/5/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> >’: 
/usr/include/c++/5/type_traits:137:12: required from ‘struct std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > >’ 
/usr/include/c++/5/type_traits:148:38: required from ‘struct std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
/usr/include/c++/5/bits/unordered_map.h:100:66: required from ‘class std::unordered_map<boost::any, boost::any>’ 
any.cc:5:48: required from here 
/usr/include/c++/5/bits/hashtable_policy.h:85:34: error: no match for call to ‘(const std::hash<boost::any>) (const boost::any&)’ 
    noexcept(declval<const _Hash&>()(declval<const _Key&>()))> 
           ^
In file included from /usr/include/c++/5/bits/move.h:57:0, 
       from /usr/include/c++/5/bits/stl_pair.h:59, 
       from /usr/include/c++/5/utility:70, 
       from /usr/include/c++/5/unordered_map:38, 
       from any.cc:1: 
/usr/include/c++/5/type_traits: In instantiation of ‘struct std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’: 
/usr/include/c++/5/bits/unordered_map.h:100:66: required from ‘class std::unordered_map<boost::any, boost::any>’ 
any.cc:5:48: required from here 
/usr/include/c++/5/type_traits:148:38: error: ‘value’ is not a member of ‘std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > >’ 
    : public integral_constant<bool, !_Pp::value> 
            ^
In file included from /usr/include/c++/5/unordered_map:48:0, 
       from any.cc:1: 
/usr/include/c++/5/bits/unordered_map.h: In instantiation of ‘class std::unordered_map<boost::any, boost::any>’: 
any.cc:5:48: required from here 
/usr/include/c++/5/bits/unordered_map.h:100:66: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef __umap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc> _Hashtable; 
                   ^
/usr/include/c++/5/bits/unordered_map.h:107:45: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::key_type key_type; 
              ^
/usr/include/c++/5/bits/unordered_map.h:108:47: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::value_type value_type; 
              ^
/usr/include/c++/5/bits/unordered_map.h:109:48: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::mapped_type mapped_type; 
               ^
/usr/include/c++/5/bits/unordered_map.h:110:43: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::hasher hasher; 
             ^
/usr/include/c++/5/bits/unordered_map.h:111:46: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::key_equal key_equal; 
              ^
/usr/include/c++/5/bits/unordered_map.h:112:51: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::allocator_type allocator_type; 
               ^
/usr/include/c++/5/bits/unordered_map.h:117:45: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::pointer pointer; 
              ^
/usr/include/c++/5/bits/unordered_map.h:118:50: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::const_pointer const_pointer; 
               ^
/usr/include/c++/5/bits/unordered_map.h:119:47: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::reference reference; 
              ^
/usr/include/c++/5/bits/unordered_map.h:120:52: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::const_reference const_reference; 
                ^
/usr/include/c++/5/bits/unordered_map.h:121:46: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::iterator iterator; 
              ^
/usr/include/c++/5/bits/unordered_map.h:122:51: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::const_iterator const_iterator; 
               ^
/usr/include/c++/5/bits/unordered_map.h:123:51: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::local_iterator local_iterator; 
               ^
/usr/include/c++/5/bits/unordered_map.h:124:57: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::const_local_iterator const_local_iterator; 
                 ^
/usr/include/c++/5/bits/unordered_map.h:125:47: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::size_type size_type; 
              ^
/usr/include/c++/5/bits/unordered_map.h:126:52: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     typedef typename _Hashtable::difference_type difference_type; 
                ^
/usr/include/c++/5/bits/unordered_map.h:280:7: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     operator=(initializer_list<value_type> __l) 
    ^
/usr/include/c++/5/bits/unordered_map.h:379:2: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
    emplace(_Args&&... __args) 
^
/usr/include/c++/5/bits/unordered_map.h:432:7: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     insert(const value_type& __x) 
    ^
/usr/include/c++/5/bits/unordered_map.h:439:2: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
    insert(_Pair&& __x) 
^
/usr/include/c++/5/bits/unordered_map.h:499:7: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     insert(initializer_list<value_type> __l) 
    ^
/usr/include/c++/5/bits/unordered_map.h:645:7: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     equal_range(const key_type& __x) 
    ^
/usr/include/c++/5/bits/unordered_map.h:649:7: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<boost::any> >, std::__detail::__is_noexcept_hash<boost::any, std::hash<boost::any> > > >’ 
     equal_range(const key_type& __x) const 
    ^

但是當我使用boost::unordered_map而非std,它工作沒有任何錯誤。

爲什麼?

+0

我知道消息很大,這讓人很想放棄,但通常這些錯誤的關鍵在於尋找你不認識和/或沒有要求的類型。正如答案所解釋的,如果你只是讀了一點,你會看到對'std :: hash'的引用,你沒有編碼,因此表示'map'試圖使用它。 –

+0

@underscore_d好吧,我知道我很困惑,像你的評論,並沒有努力。謝謝。 – signal

回答

4

std::unordered_map使用std::hash作爲默認散列器。對於boost::any沒有專門的std::hash。因爲STanDard庫不知道來自第三方庫的任何實體。

boost::unordered_map使用boost::hash作爲默認散列器,該散列器專門針對boost庫的類型。

解決方法:明確告知std::unordered_map使用boost::hash<boost::any>作爲hasher。

+0

謝謝。它幫助我理解。 – signal

1

當您聲明unordered_map<Key, Value>時,您需要散列函數。默認的哈希函數是const hash<Key>(const Key&)

boost::any,它似乎有這樣的哈希模板專業化,但爲boost::hash

所以實際上,編譯器試圖找到一個專門爲std::hashboost::any,但只發現boost::hash專業化。

+0

謝謝你的好意。 – signal