为什么 boost::assign::list_of 不适用于对字符串、矢量<string>><?

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

本文关键字:lt gt 矢量 string 字符串 list assign boost of 适用于 不适用      更新时间:2023-10-16

我不明白为什么这不起作用(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"));
}

错误为:

includeutility(138) : error C2668: 'std::vector<_Ty>::vector' : ambiguous call to overloaded function with [ _Ty=std::string ]
includevector(786): could be 'std::vector<_Ty>::vector(std::vector<_Ty> &&)' with [ _Ty=std::string ]
includevector(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>
]

我无法理解它为什么试图访问unsigned int过载。。。有什么想法吗?

这是因为在C++11中添加了一个新的pair构造函数来接受通用引用。因此,当在C++11模式下时,此代码将在VS2012(添加了此构造函数)和GCC中失败。

在C++03中

pair<T1,T2>构造函数是:

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

在这种情况下,T2 == vector<string>

generic_list对象(list_of返回的对象)有一个模板转换运算符:

template <class Container>
operator Container() const;

当您传入generic_list作为参数时,它会尝试将generic_list对象转换为vector<string>,因为这是构造函数所期望的,并且成功了。

在C++11中

添加了此pair<T1,T2>构造函数:

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

现在,当传入generic_list对象时,它将作为generic_list&&传入。当它试图用这个对象调用second(类型为vector<string>)构造函数时,它不知道该调用以下构造函数中的哪一个:

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

由于CCD_ 15可以被转换为CCD_ 16和CCD_。这会导致编译错误。

修复/解决方案

一个可能的解决方案是使用convert_to_container方法并指定目标类型:

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

另一种选择是使用make_pair并显式指定其模板参数。

所以不是这样:

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

我必须写:

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

让它有点不可读。。。我添加了另一个模板:

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

现在可以写为:

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

这仍然不好,但更接近于你开始的。