簡體   English   中英

為什么boost :: assign :: list_of不能使用pair <string,vector <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"));
}

錯誤是:

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>
]

我無法破譯為什么它試圖訪問unsigned int overload ...任何想法?

這是因為在C ++ 11中添加了pair新的構造函數來接受通用引用。 因此,此代碼將在VS2012(添加此構造函數)和在C ++ 11模式下的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);

由於generic_list可以轉換為size_typevector<string> 這會導致編譯錯誤。

修復/解決方法

可能的解決方法是使用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")))

這仍然不是很好,但更接近你的開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM