2012-12-18 11 views
6

मुझे समझ नहीं आता क्यों यह (विजुअल C++ 2012) काम नहीं करता:क्यों बढ़ावा :: असाइन :: list_of जोड़ी <स्ट्रिंग, वेक्टर <string>> के साथ काम नहीं कर रहा है?

#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 अधिभार का उपयोग करने की कोशिश कर रहा है ... कोई विचार?

+0

क्योंकि टेम्पलेट कन्स्ट्रक्टर भी उपयुक्त नहीं है। आज़माएं: जोड़ी <स्ट्रिंग, वेक्टर > ("^", boost :: असाइन :: list_of (std :: string ("नियम")); या जोड़ी <स्ट्रिंग, वेक्टर > ("^", boost :: असाइन :: list_of ("नियम")); – kassak

+0

@ डाउनवॉटर: डाउनवोट क्यों? – Mehrdad

+0

@ कसक: क्यों 'हस्ताक्षरित int' क्यों? मुझे समस्या के आसपास काम करने में बहुत परेशानी नहीं है, बल्कि यह समझना कि यह पहली जगह क्यों हो रहा है। – Mehrdad

उत्तर

6

ऐसा इसलिए है क्योंकि सार्वभौमिक संदर्भों को स्वीकार करने के लिए एक नया pair कन्स्ट्रक्टर सी ++ 11 में जोड़ा गया था। नतीजतन, यह कोड वीएस2012 (जो इस कन्स्ट्रक्टर को जोड़ा गया) में विफल होगा और जीसीसी में जब सी ++ 11 मोड में होगा।

सी ++ 03 में:

pair<T1,T2> निर्माता था:

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

इस मामले में, T2 == vector<string>। ,

template <class Container> 
operator Container() const; 

जब आप एक पैरामीटर के रूप generic_list में पारित, यह एक vector<string> को generic_list वस्तु कन्वर्ट करने के लिए कोशिश करता है के बाद से वह यह है कि:

एक generic_list वस्तु (वस्तु list_of द्वारा दिया) एक टेम्पलेट रूपांतरण ऑपरेटर है निर्माता क्या अपेक्षा करता है, और यह सफल होता है।

सी ++ 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_type और vector<string> दोनों के लिए परिवर्तित किया जा सकता है। यह संकलन त्रुटि में परिणाम।

फिक्स/वर्कअराउंड:

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

एक अन्य विकल्प make_pair उपयोग करने के लिए और स्पष्ट रूप से अपने टेम्पलेट पैरामीटर निर्दिष्ट है:

एक संभव फिक्स convert_to_container विधि का उपयोग करें और लक्ष्य के प्रकार का उल्लेख है।

+0

कुछ मिनट पहले मैंने सोचा था कि आप समस्या को दबाएंगे, लेकिन अब मुझे इतना यकीन नहीं है। 'जोड़ी <स्ट्रिंग, वेक्टर > ("^", boost :: असाइन :: list_of ("नियम") क्यों होगा;' वेक्टर 'के स्पष्ट कन्स्ट्रक्टर को कॉल करना होगा? – Mehrdad

+0

@ मेहरदाद: आप सही हैं, मैं इसके बारे में गलत था। मैंने अपना जवाब बदल दिया और मुझे लगता है कि यह अभी सही है। – interjay

+1

अहह, इसलिए यह अभी भी स्पष्ट निर्माता के साथ एक समस्या है, लेकिन यह अग्रेषण कन्स्ट्रक्टर के कारण है! दिलचस्प, धन्यवाद। वैसे, मुझे आश्चर्य है कि क्या इसका वास्तव में मतलब है कि सी ++ में "परिपूर्ण" अग्रेषण अभी भी असंभव है, यह देखते हुए कि यह आपको स्पष्ट नस्ल को संभालने नहीं देता है? = ओ शायद मुझे इसे एक प्रश्न के रूप में पोस्ट करना चाहिए! – Mehrdad

1

बजाय इस की:

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

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

मैं लिखने के लिए है और अब इसे लिख सकते हैं:

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

जो अभी भी अच्छा नहीं है, लेकिन आपने जो शुरू किया उसके करीब है।

संबंधित मुद्दे