使用 boost::assign 将 std::set 嵌套在 std::map 中

4
我正在尝试使用boost::assign来模拟C++11初始化包含std::setstd::map
#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>]

我该如何解决这个模糊性错误?

list_of(10U)(20U)(30U) 有帮助吗? - aschepler
1个回答

3
在这种情况下,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中的一个bug,请参见:http://boost.2283326.n4.nabble.com/Typedef-rejected-when-disambiguating-a-call-to-boost-assign-td4637775.html。 - mark

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接