为什么 boost::assign::list_of 不适用于对字符串、矢量<string>><?
Why boost::assign::list_of isn't working with pair<string, vector<string>>?
我不明白为什么这不起作用(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")))
这仍然不好,但更接近于你开始的。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中