6

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?

user541686
  • 205,094
  • 128
  • 528
  • 886
  • That because template constructor is not suitable too. Try: pair >("^", boost::assign::list_of(std::string("rules"))); or pair >("^", boost::assign::list_of("rules")); – kassak Dec 18 '12 at 08:01
  • @kassak: Why `unsigned int` though? I'm not having much trouble working around the problem, but rather understanding why it's happening in the first place. – user541686 Dec 18 '12 at 08:08
  • Compiler could not find suitable constructor and just lists all one-argument constructors. And it could not find constructor, because result of list_of is not convertiable to vector – kassak Dec 18 '12 at 08:11
  • @kassak: `pair >("^", boost::assign::list_of("rules"))` doesn't work either; I'll update the question. – user541686 Dec 18 '12 at 08:51
  • hm...that works fine for me in vs2008 – kassak Dec 18 '12 at 09:16
  • Very strange. Does explicit cast helps? pair > aa("^", (vector)boost::assign::list_of("rules")) Not as solution, just test =) – kassak Dec 18 '12 at 09:22
  • @kassak: Almost... now I get `test.cpp(13) : error C2440: 'type cast' : cannot convert from 'boost::assign_detail::generic_list' to 'std::vector<_Ty>' with [ T=std::string ] and [ _Ty=std::string ] No constructor could take the source type, or constructor overload resolution was ambiguous`. It seems fine on 2008 here too, but 2010 and 2012 give these errors. – user541686 Dec 18 '12 at 09:58

2 Answers2

6

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.

interjay
  • 107,303
  • 21
  • 270
  • 254
  • A couple minutes ago I thought you'd nailed the problem, but now I'm not so sure. Why would `pair >("^", boost::assign::list_of("rules"));` be calling `vector`'s explicit constructor? – user541686 Dec 19 '12 at 01:10
  • @Mehrdad: You're right, I was wrong about that. I changed my answer and I think it's correct now. – interjay Dec 19 '12 at 10:18
  • 1
    Ahhhh, so it's still a problem with the explicit constructor, but it's due to the forwarding constructor! Interesting, thanks. By the way, I wonder if this really means that "perfect" forwarding is still impossible in C++, given that it doesn't let you handle explicit-ness? =O Maybe I should post that as a question! – user541686 Dec 19 '12 at 10:21
  • Funny, before I figure that out, I ran into another weird issue. Here's a link if you're curious/interested: http://stackoverflow.com/questions/13950556 – user541686 Dec 19 '12 at 10:44
  • @Mehrdad: The constructor being `explicit` isn't relevant, the same would happen without `explicit`. The issue is that in the C++03 version, an implicit conversion is performed from `generic_list` to `vector`, which can only happen by converting it directly to a vector, since an implicit conversion can only do one conversion. In the C++11 version the `vector` constructor is called explicitly, so a conversion can be done from `generic_list` to either `vector` or `size_type`. – interjay Dec 19 '12 at 11:39
1

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.

Hsch
  • 11
  • 1