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

Baz*_*Baz 6 c++ boost c++98

这段代码:

std::vector<int>(boost::assign::list_of<int>(1)(2)(3));
Run Code Online (Sandbox Code Playgroud)

给出错误:

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>]
Run Code Online (Sandbox Code Playgroud)

当为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()));
Run Code Online (Sandbox Code Playgroud)

出于同样的原因,这也失败了.

我的应用程序涉及为单元测试设置数据结构.我希望初始化清晰,而不是必须做很多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()));
Run Code Online (Sandbox Code Playgroud)

这太丑了.我该怎么做才能让这个更清楚.我的单元测试只写在头文件中,所以我不使用using namespace.

Eri*_*ler 3

这是VS2010 中使用 list_of 进行模糊调用的欺骗。Boost.Assign 是针对 C++03 标准库编写的。C++11 中的情况发生了变化,Boost.Assign 尚未更新。错误报告在这里