2016-07-29 114 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