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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With