Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguity when using boost::assign::list_of to construct a std::vector

Tags:

c++

boost

c++98

This code:

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

gives the error:

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>]

when compiled for gcc 4.4.2.

How can I revolve this issue? Ultimately I'm trying to compile the following:

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()));

This fails for the same reason.

My application involves setting up data structures for the purposes of unit testing. I want the initialization to be clear rather that having to do lots of push_backs etc.

UPDATE:

Here is a fix:

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()));

This is so ugly. What might I do to make this clearer. My unit tests are written only in header files so I don't use using namespace.

like image 489
Baz Avatar asked Apr 25 '13 09:04

Baz


1 Answers

This is a dupe of Ambiguous call with list_of in VS2010. Boost.Assign was written targeting the C++03 standard library. Things changed in C++11 and Boost.Assign hasn't been updated. The bug report is here.

like image 142
Eric Niebler Avatar answered Oct 20 '22 00:10

Eric Niebler