Meh*_*dad 6 c++ boost visual-c++
我不明白为什么这不起作用(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"));
}
Run Code Online (Sandbox Code Playgroud)
错误是:
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>
]
Run Code Online (Sandbox Code Playgroud)
我无法破译为什么它试图访问unsigned int超载...任何想法?
这是因为pair在C++ 11中添加了一个新的构造函数来接受通用引用.因此,此代码将在VS2012(添加此构造函数)和在C++ 11模式下的GCC中失败.
在C++ 03中:
该pair<T1,T2>构造函数是:
pair( const T1& x, const T2& y ) : first(x), second(y) {}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,T2 == vector<string>.
甲generic_list对象(通过返回的对象list_of)有一个模板转换运算符:
template <class Container>
operator Container() const;
Run Code Online (Sandbox Code Playgroud)
当您generic_list作为参数传入时,它会尝试将generic_list对象转换为a 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))
Run Code Online (Sandbox Code Playgroud)
现在当你传入一个generic_list对象时,它将作为传入generic_list&&.当它尝试使用此对象调用second(类型vector<string>)构造函数时,它不知道要调用哪些构造函数:
explicit vector(size_type count, [more params with default values])
vector(const vector& other);
Run Code Online (Sandbox Code Playgroud)
既然generic_list可以转换为size_type和vector<string>.这会导致编译错误.
修复/解决方法:
可能的解决convert_to_container方法是使用该方法并指定目标类型:
pair<string, vector<string> >("^", boost::assign::list_of<string>("rules").convert_to_container<vector<string> >());
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用make_pair并明确指定其模板参数.
| 归档时间: |
|
| 查看次数: |
3163 次 |
| 最近记录: |