c++ - 使用 boost::assign 和嵌套在 std::map 中的 std::set

标签 c++ boost

我正在尝试使用 boost::assign模拟 std::map 的 C++11 初始化包含 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) );

std::set 的初始化使用 boost::assign::list_of单独使用时按预期工作,但当我尝试上面的代码时,赋值在 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>]

如何解决这个歧义错误?

最佳答案

在这种情况下 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> >(...);

关于c++ - 使用 boost::assign 和嵌套在 std::map 中的 std::set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13125924/

相关文章:

c++ - 使用对象中的方法调用带有 std::bind 和 std::function.target 的 C 风格函数地址

c++ - 当我使用 -isystem 标志而不是 INCLUDEPATH 时,QtCreator 的代码检查器中断

c++ - std::optional 从方法返回可选值的最佳替代方案? (使用 C++98/C++11/C++14)

c++ - 在对 vector 中查找函数时出错

c++ - 如何通过流插入运算符调用成员函数?

c++ - 循环字符串以查看字符串失败的位置

c++ - 是否可以使 std::string 始终包含小写字符串?

c++ - 使用 Boost 公开静态常量

c++ - 递归创建 boost 属性树

c++ - 如何捕获 C++ 标准 17.6.4.10 [res.on.exception.handling] 中描述的异常?