2012-10-29 65 views
3

我想用boost::assign来模拟包含std::setstd::map的C++ 11初始化。如预期使用boost :: assign与嵌套在std :: map中的std :: set

#include <set> 
#include <map> 
#include <stdint.h> 

#include <boost/assign/list_of.hpp> 

typedef std::map< uint32_t, std::set< uint32_t> > the_map_t; 

the_map_t data = boost::assign::map_list_of(1, boost::assign::list_of(10)(20)(30)) 
              (2, boost::assign::list_of(12)(22)(32)) 
              (3, boost::assign::list_of(13)(23)(33)) 
              (4, boost::assign::list_of(14)(24)(34)); 

的使用boost::assign::list_of作品std::set初始化单独使用时,但是当我尝试上面的代码分配是不确定的,其中std::set的构造函数调用点:

map-assign.cpp:16: instantiated from here 
include/c++/4.4.6/bits/stl_pair.h:101: error: call of overloaded set(const boost::assign_detail::generic_list<int>&) is ambiguous 
include/c++/4.4.6/bits/stl_set.h:188: note: candidates are: 
    std::set<_Key, _Compare, _Alloc>::set(
     const std::set<_Key, _Compare, _Alloc>&) 
     [with _Key = unsigned int, _Compare = std::less<unsigned int>, _Alloc = std::allocator<unsigned int>] 

include/c++/4.4.6/bits/stl_set.h:145: note:     
    std::set<_Key, _Compare, _Alloc>::set(
     const _Compare&, const _Alloc&) 
     [with _Key = unsigned int, _Compare = std::less<unsigned int>, _Alloc = std::allocator<unsigned int>] 

如何解决此歧义错误?

+0

是否'LIST_OF(10U)(20U)(30U)'帮助呢? – aschepler

回答

2

在这种情况下,boost::assign::map_list_of需要提示第二个模板参数 - <uint32_t, std::set< uint32_t> >。因此线

the_map_t data = boost::assign::map_list_of(...); 

成为

the_map_t data = boost::assign::map_list_of<uint32_t, std::set< uint32_t> >(...); 
+1

干杯,但我相信已经发现'boost :: assign'内的错误,请参阅:http://boost.2283326.n4.nabble.com/Typedef-rejected-when-disambiguating-a-call-to-boost -assign-td4637775.html。 – mark