这段代码:
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
.