Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why boost::assign::list_of isn't working with pair<string, vector<string>>?

I don't understand why this doesn't work (Visual C++ 2012):

#include <string>
#include <utility>
#include <vector>
#include <boost/assign/list_of.hpp>

using namespace std;

int main()
{
    pair<string, vector<string> >("^", boost::assign::list_of<string>("rules"));
}

The error is:

include\utility(138) : error C2668: 'std::vector<_Ty>::vector' : ambiguous call to overloaded function with [ _Ty=std::string ]
include\vector(786): could be 'std::vector<_Ty>::vector(std::vector<_Ty> &&)' with [ _Ty=std::string ]
include\vector(693): or       'std::vector<_Ty>::vector(unsigned int)' with [ _Ty=std::string ]
while trying to match the argument list '(boost::assign_detail::generic_list<T>)' with [ T=std::string ]
test.cpp(12) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<const char(&)[2],boost::assign_detail::generic_list<T>>(_Other1,_Other2 &&,void **)' being compiled
with
[
    _Ty1=std::string,
    _Ty2=std::vector<std::string>,
    T=std::string,
    _Other1=const char (&)[2],
    _Other2=boost::assign_detail::generic_list<std::string>
]
test.cpp(12) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<const char(&)[2],boost::assign_detail::generic_list<T>>(_Other1,_Other2 &&,void **)' being compiled
with
[
    _Ty1=std::string,
    _Ty2=std::vector<std::string>,
    T=std::string,
    _Other1=const char (&)[2],
    _Other2=boost::assign_detail::generic_list<std::string>
]

I can't decipher why it's trying to access an unsigned int overload... any ideas?

like image 650
user541686 Avatar asked Dec 18 '12 07:12

user541686


2 Answers

This is because a new pair constructor was added in C++11 to accept universal references. As a result, this code will fail in VS2012 (which added this constructor) and in GCC when in C++11 mode.

In C++03:

The pair<T1,T2> constructor was:

pair( const T1& x, const T2& y ) : first(x), second(y) {}

In this case, T2 == vector<string>.

A generic_list object (the object returned by list_of) has a template conversion operator:

template <class Container>
operator Container() const;

When you pass in generic_list as a parameter, it tries to convert the generic_list object to a vector<string>, since that is what the constructor expects, and this succeeds.

In C++11:

This pair<T1,T2> constructor was added:

template< class U1, class U2 >
pair( U1&& x, U2&& y ) : first(std::forward<U1>(x)), second(std::forward<U2>(y))

Now when you pass in a generic_list object, it will be passed in as generic_list&&. When it tries to call the second (of type vector<string>) constructor with this object, it doesn't know which of these constructors to call:

explicit vector(size_type count, [more params with default values])
vector(const vector& other);

Since generic_list can be converted to both size_type and vector<string>. This results in the compilation error.

Fix/Workaround:

A possible fix is to use the convert_to_container method and specify the target type:

pair<string, vector<string> >("^", boost::assign::list_of<string>("rules").convert_to_container<vector<string> >());

Another option is to use make_pair and explicitly specify its template parameters.

like image 141
interjay Avatar answered Nov 14 '22 23:11

interjay


So instead of this:

("^", boost::assign::list_of<string>("rules"))

I have to write:

("^", boost::assign::list_of<string>("rules").convert_to_container<vector<string> >());

Makes it kind of unreadable ... I added Yet Another template:

template <typename T>
std::vector<T> vect(const boost::assign_detail::generic_list<T>& gen_list)
{ return gen_list.convert_to_container<std::vector<T> >(); }

and can write it now as:

("^", vect(boost::assign::list_of<string>("rules")))

which is still not nice, but closer to what you started with.

like image 39
Hsch Avatar answered Nov 14 '22 22:11

Hsch