2014-09-18 8 views
14

इस कोड टुकड़ा:इस वेक्टर कन्स्ट्रक्टर में तर्क के रूप में क्यों enums का उपयोग नहीं किया जा सकता है?

enum {N = 10, M = 100}; 

vector<int> v(N, M); 

, विजुअल स्टूडियो 2013 के साथ संकलित करने के लिए विफल रहता है निम्न त्रुटि के कारण:

error C2838: 'iterator_category' : illegal qualified name in member declaration

इसके साथ क्या गलत है?

+2

दोनों जीसीसी और क्लैंग ठीक हैं, यह एक बग की तरह लगता है। –

+0

मुझे दृढ़ता से वीएस की बग पर संदेह है .. –

उत्तर

12

यह वीएस2012 और वीएस2013 दोनों में एक बग है क्योंकि यह सी ++ 11 मानक (_HAS_CPP0X के साथ परिभाषित नहीं है) के रूप में परिभाषित है 1):

सी ++ 03 23.1.1 [lib.sequence.reqmts]/9 का कहना है:

:

For every sequence defined in this clause and in clause 21:

— the constructor template <class InputIterator> X(InputIterator f, InputIterator l, const Allocator& a = Allocator()) shall have the same effect as:

X(static_cast<typename X::size_type>(f), static_cast<typename X::value_type>(l), a) if InputIterator is an integral type.

लेकिन सी ++ 11 23.2.3 [sequence.reqmts]/14 से

For every sequence container defined in this Clause and in Clause 21:

— If the constructor template <class InputIterator> X(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()) is called with a type InputIterator that does not qualify as an input iterator, then the constructor shall not participate in overload resolution.

निर्माता सभी

अधिक यहाँ पर विचार नहीं किया जाना चाहिए था कि किया गया: https://stackoverflow.com/a/12432482/1938163

एक कामकाज के रूप में आप "ओवरलोड रिज़ॉल्यूशन को थोड़ा" में सहायता कर सकते हैं, उदा।

std::vector<int> v(static_cast<std::vector<int>::size_type>(N), M); 
10

चूंकि सी ++ 11 vector दो InputIterators स्वीकार करने वाले कन्स्ट्रक्टर को अक्षम किया जाना चाहिए यदि दो तर्क इटरेटर नहीं हैं। VS2013 इसे सही ढंग से कार्यान्वित करने में विफल रहता है।

[...]

see reference to function template instantiation 'std::vector>::vector<,void>(_Iter,_Iter)' being compiled

[...]>

यह निर्माता है कि दो इनपुट iterators लेता उपयोग करने के लिए प्रयास कर रहा है:

2

यह एक विजुअल स्टूडियो 2013 बग, त्रुटि से उत्पन्न किया जा रहा (see it live), यह सिर्फ एक छोटा सा हिस्सा है। कौन सा बग होगा, gcc और clang दोनों इस कोड के साथ ठीक हैं।

हम उस में सी ++ 11 that constructor नहीं माना जाना चाहिए कर सकते हैं:

Constructs the container with the contents of the range [first, last). This constructor does not participate in overload resolution if InputIt does not satisfy InputIterator, to avoid ambiguity with the overload 2 (since C++11)

यह मसौदा सी ++ 11 मानक खंड 23.2.3अनुक्रम कंटेनर पैरा जो कहते हैं के साथ सहमत हैं:

If the constructor

template <class InputIterator> 
X(InputIterator first, InputIterator last, 
    const allocator_type& alloc = allocator_type()) 

is called with a type InputIterator that does not qualify as an input iterator, then the constructor shall not participate in overload resolution.

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

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