加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – Boost快速池分配器是否存在函数指针问题?

发布时间:2020-12-16 06:59:50 所属栏目:百科 来源:网络整理
导读:这是一个Boost错误还是我做错了什么? #include map#include boost/pool/pool_alloc.hppint main(){ typedef const std::string key; typedef double* (*value)(const int); std::mapkey,value,std::lesskey map_with_standard_allocator; // works std::map
这是一个Boost错误还是我做错了什么?

#include <map>
#include <boost/pool/pool_alloc.hpp>

int main()
{
    typedef const std::string key;
    typedef double* (*value)(const int&);
    std::map<key,value,std::less<key>> map_with_standard_allocator; // works
    std::map<key,std::less<key>,boost::fast_pool_allocator<std::pair<const key,value> > > map_with_boost_allocator; // fails
}

最后一行无法在MS Visual Studio 2008下使用Boost 1.40和1.48进行编译.然而,它在g 4.5.3(Cygwin)下编译得很好.

错误是:

1>Compiling...
1>main.cpp
1>C:UniLib1trunkExternalboost/pool/pool_alloc.hpp(205) : error C2535: 'const std::basic_string<_Elem,_Traits,_Ax> *boost::fast_pool_allocator<T,UserAllocator,Mutex,NextSize>::address(const std::basic_string<_Elem,_Ax> &)' : member function already defined or declared
1>        with
1>        [
1>            _Elem=char,1>            _Traits=std::char_traits<char>,1>            _Ax=std::allocator<char>,1>            T=const std::basic_string<char,std::char_traits<char>,std::allocator<char>>,1>            UserAllocator=boost::default_user_allocator_new_delete,1>            Mutex=boost::details::pool::default_mutex,1>            NextSize=32
1>        ]
1>        C:UniLib1trunkExternalboost/pool/pool_alloc.hpp(202) : see declaration of 'boost::fast_pool_allocator<T,NextSize>::address'
1>        with
1>        [
1>            T=const std::basic_string<char,1>            NextSize=32
1>        ]
1>        c:Program FilesMicrosoft Visual Studio 9.0VCincludextree(137) : see reference to class template instantiation 'boost::fast_pool_allocator<T,NextSize>' being compiled
1>        with
1>        [
1>            T=const std::basic_string<char,1>            NextSize=32
1>        ]
1>        c:Program FilesMicrosoft Visual Studio 9.0VCincludemap(78) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=std::_Tmap_traits<key,boost::fast_pool_allocator<std::pair<key,value >>,false>
1>        ]
1>        .main.cpp(9) : see reference to class template instantiation 'std::map<_Kty,_Ty,_Pr,_Alloc>' being compiled
1>        with
1>        [
1>            _Kty=key,1>            _Ty=value,1>            _Pr=std::less<key>,1>            _Alloc=boost::fast_pool_allocator<std::pair<key,value >>
1>        ]

解决方法

这不是VS2008中的一个错误(因为我在此答案的早期编辑中错误地声称). C 03标准要求关联容器的密钥类型(如std :: map)必须是“可分配的”(根据23.1.2“关联容器”中的表69).并且const std :: string不可分配.请注意,C 11标准似乎放宽了这一要求,但新标准不适用于VC 2008以来.

我不清楚是否需要编译器来诊断尝试使用std :: map和不可分配密钥的代码,所以我认为没有人可以声称GCC或VC 2010不正确地接受此代码(我认为它属于未定义代码区域,可以按照您的预期工作,即使不能保证它能正常工作).但很明显,VC 2008拒绝编译它是可以的.

总而言之,我认为VC 2008的库参数化地图键上的分配器地址()函数而不是map的元素仍然是可疑的(如果你感兴趣的话,请参阅这个答案的第一个编辑,但是我不知道)认为自从std :: pair<>之后就有任何真正的错误用于保持map元素的用法总是被设置为使得key部分与整个元素位于同一地址.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读