2016-09-13 10 views
8

नीचे दिया गया कोड ArchLinux पर clang 3.8.1-1 पर सही ढंग से संकलित करता है।फ़ंक्शन टेम्पलेट शीर्ष-स्तरीय कॉन्स के साथ घोषित पैरामीटर को संशोधित करता है: क्लैंग बग?

क्या यह clang बग है?

gcc इस पर सही चेतावनी/त्रुटि जारी करें।

template <class T> 
struct BugReproducer{ 
    using size_type = typename T::size_type; 

    int bug1(size_type count); 
    int bug2(size_type count) const; 
    static int bug3(size_type count); 
}; 

template <class T> 
int BugReproducer<T>::bug1(size_type const count){ 
    // this is a bug. must be not allowed 
    count = 5; 

    // return is to use the result... 
    return count; 
} 

template <class T> 
int BugReproducer<T>::bug2(size_type const count) const{ 
    // same for const method 
    count = 5; 
    return count; 
} 

template <class T> 
int BugReproducer<T>::bug3(size_type const count){ 
    // same for static method 
    count = 5; 
    return count; 
} 



struct DummyVector{ 
    using size_type = int; 
}; 



int main(){ 
    using BugRepr = BugReproducer<DummyVector>; 

    BugRepr reproducer; 

    auto a = reproducer.bug1(1); 
    auto b = reproducer.bug2(1); 
    auto c = BugRepr::bug3(1); 

    // return is to use the result... 
    return a + b + c; 
} 

यहाँ मैं कैसे संकलन:

[[email protected] HM3]$ clang x.cc -std=c++11 -lstdc++ -Wall -Wpedantic -Wconversion 

clang और c++14 - एक ही परिणाम।

[[email protected] HM3]$ clang x.cc -std=c++14 -lstdc++ -Wall -Wpedantic -Wconversion 

यहाँ जीसीसी उत्पादन होता है:

[[email protected] HM3]$ gcc x.cc -std=c++11 -lstdc++ -Wall -Wpedantic -Wconversion 
x.cc: In instantiation of ‘int BugReproducer<T>::bug1(BugReproducer<T>::size_type) [with T = DummyVector; BugReproducer<T>::size_type = int]’: 
x.cc:46:28: required from here 
x.cc:13:8: error: assignment of read-only parameter ‘count’ 
    count = 5; 
    ~~~~~~^~~ 
x.cc: In instantiation of ‘int BugReproducer<T>::bug2(BugReproducer<T>::size_type) const [with T = DummyVector; BugReproducer<T>::size_type = int]’: 
x.cc:47:28: required from here 
x.cc:22:8: error: assignment of read-only parameter ‘count’ 
    count = 5; 
    ~~~~~~^~~ 
x.cc: In instantiation of ‘static int BugReproducer<T>::bug3(BugReproducer<T>::size_type) [with T = DummyVector; BugReproducer<T>::size_type = int]’: 
x.cc:48:20: required from here 
x.cc:29:8: error: assignment of read-only parameter ‘count’ 
    count = 5; 
    ~~~~~~^~~ 
+10

"क्लैंग बग रिपोर्ट" के लिए Google खोज करें। उपरोक्त समझ में नहीं आता है। ऑफ़-साइट संसाधनों के लिए दिशा-निर्देशों का अनुरोध यहां स्पष्ट रूप से ऑफ-विषय है। –

+0

मैं संपादित कर दूंगा और अनुरोध को हटा दूंगा। लेकिन क्या यह एक बग है? – Nick

+0

मैं नहीं देख सकता कि यह एक बग क्यों नहीं है - लेकिन एक रिपोर्ट क्यों न दर्ज करें, और देखें कि क्या झगड़ा ट्रायज़ कहता है? –

उत्तर

5

हाँ, यह बजना में एक बग है; https://llvm.org/bugs/show_bug.cgi?id=30365 पर दायर किया गया।

बग की प्रकृति है कि एक वर्ग टेम्पलेट सदस्य समारोह परिभाषा के बाहर दिखाई दे रहा ([class.mfct]/1) वर्ग टेम्पलेट, एक पैरामीटर के प्रकार के वर्ग टेम्पलेट मापदंडों पर निर्भर के साथ, बजना का उपयोग करता है है परिभाषा के पैरामीटर प्रकार के बजाय घोषणापत्र का पैरामीटर प्रकार जहां वे शीर्ष सीवी-योग्यता में भिन्न होते हैं। सरलीकृत उदाहरण:

template<class T> struct A { void f(typename T::U); }; 
template<class T> void A<T>::f(typename T::U const i) { i = 1; } 
struct X { using U = int; }; 
int main() { A<X>{}.f(0); } 
प्रति

[dcl.fct]/5 A<X>::f की परिभाषा के भीतर i के प्रकार int const (Use of 'const' for function parameters) है:

5 - [...] के उत्पादन के बाद पैरामीटर प्रकारों की सूची, किसी भी शीर्ष-स्तरीय सीवी-क्वालीफायर पैरामीटर प्रकार को संशोधित करते समय फ़ंक्शन प्रकार बनाते समय हटा दिया जाता है। [...] [नोट: यह परिवर्तन पैरामीटर के प्रकारों को प्रभावित नहीं करता है। [...] - अंत नोट]

+0

धन्यवाद। क्या आपने इसकी सूचना दी? मैं अभी भी बग खाते की प्रतीक्षा कर रहा हूं। – Nick

+0

हां, मैंने इसकी सूचना दी है - उपरोक्त लिंक जोड़ा गया। – ecatmur

+0

धन्यवाद। मैं आपके द्वारा किए गए कोड को कम करने में भी असमर्थ था। – Nick

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