c++ - 使用 boost::assign::list_of 构造 std::vector 时的歧义

标签 c++ boost c++98

这段代码:

std::vector<int>(boost::assign::list_of<int>(1)(2)(3));

给出错误:

main.cpp: In member function 'void <unnamed>::RequestHandler::processRequest(Foo&, Bar, unsigned int, unsigned int*, const char*, boost::shared_ptr<Baz::IOutput>&)':
main.cpp:450: error: call of overloaded 'vector(boost::assign_detail::generic_list<int>&)' is ambiguous
/4.4.2/bits/stl_vector.h:241: note: candidates are: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = int, _Alloc = std::allocator<int>]
/4.4.2/bits/stl_vector.h:227: note:                 std::vector<_Tp, _Alloc>::vector(size_t, const _Tp&, const _Alloc&) [with _Tp = int, _Alloc = std::allocator<int>]
/4.4.2/bits/stl_vector.h:215: note:                 std::vector<_Tp, _Alloc>::vector(const _Alloc&) [with _Tp = int, _Alloc = std::allocator<int>]

为 gcc 4.4.2 编译时。

我该如何解决这个问题?最终我试图编译以下内容:

using namespace std;
using namespace boost::assign;

typedef boost::variant<vector<bool>, vector<int>, vector<string> > Container;

vector<pair<string, Container > > v =
            list_of(pair<string, Container >("Scotland",Container(vector<int>(list_of<int>(1)(2)(3)))))
            (pair<string, Container >("Sweden",Container()));

由于同样的原因,这失败了。

我的应用程序涉及为单元测试目的设置数据结构。我希望初始化清楚,而不是必须做很多 push_backs 等。

更新:

这是一个修复:

std::vector<std::pair<std::string, Container > > v =
    boost::assign::list_of(std::pair<std::string, Container >("Scotland",Container(boost::assign::list_of(1)(2)(3).convert_to_container<std::vector<int> >())))
    (std::pair<std::string, Container >("Sweden",Container()));

这太丑了。我可以做些什么来使这一点更清楚。我的单元测试只写在头文件中,所以我不使用 using namespace

最佳答案

这是 Ambiguous call with list_of in VS2010 的骗局. Boost.Assign 是针对 C++03 标准库编写的。 C++11 中发生了变化,Boost.Assign 尚未更新。错误报告是 here .

关于c++ - 使用 boost::assign::list_of 构造 std::vector 时的歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16211410/

相关文章:

c++ - 包含目录中的所有文件?

c++ - 如何避免隐式可转换类型

c++ - 使用 boost chrono 格式化时间

c++ - 现有的标准仿函数/函数来检查是否等于 0?

c++ - boost 随机生成器返回相同的值

c++ - CMake find_package 可以是 "common dependency version aware"吗?

android - 如何让 QVideoProbe 工作?

c++ - 使用函数 <void (boost::any)> 的事件系统是个好主意?

c++ - boost::property_map在boost中是怎么实现的,怎么改

c++ - 函数返回类型 : Pointer, 引用还是其他?