6

हम सन स्टूडियो 12.1 में माइग्रेट कर रहे हैं और नए कंपाइलर [सीसी: सन सी ++ 5.10 SunOS_sparc 2009/06/03] के साथ माइग्रेट कर रहे हैं। मुझे सन कंपाइलर के पूर्व संस्करण [सीसी: सन वर्कशॉप 6 अपडेट 2 सी ++ 5.3 2001/05/15] के साथ ठीक संकलित कोड को संकलित करते समय संकलन त्रुटि मिल रही है।सन स्टूडियो में टेम्पलेट संकलन त्रुटि 12

यह संकलन त्रुटि मुझे मिलती है।

"Sample.cc": Error: Could not find a match for LoopThrough(int[2]) needed in main(). 1 Error(s) detected. *** Error code 1.

कोड:

#include <iostream> 

#define PRINT_TRACE(STR) \ 
std::cout << __FILE__ << ":" << __LINE__ << ":" << STR << "\n"; 

template<size_t SZ> 
void LoopThrough(const int(&Item)[SZ]) 
{ 
    PRINT_TRACE("Specialized version"); 
    for (size_t index = 0; index < SZ; ++index) 
    { 
     std::cout << Item[index] << "\n"; 
    } 
} 


/*  
    template<typename Type, size_t SZ> 
    void LoopThrough(const Type(&Item)[SZ]) 
    { 
     PRINT_TRACE("Generic version");   
    } 
*/ 



int main() 
{ 
    { 
     int arr[] = { 1, 2 }; 
     LoopThrough(arr);  
    } 
} 

अगर मैं जेनेरिक संस्करण के साथ कोड uncomment, कोड ठीक संकलित करता है तथा जेनेरिक वर्जन कहा जाता है। मुझे एमएसवीसी 2010 के साथ इस समस्या को एक्सटेंशन अक्षम नहीं किया गया है और विचारधारा here के साथ एक ही मामला नहीं है। फ़ंक्शन का विशेष संस्करण कहा जाता है। अब सवाल यह है कि क्या यह सूर्य कंपाइलर में एक बग है?

यदि हां, तो हम एक बग रिपोर्ट कैसे दर्ज कर सकते हैं?

+1

शायद स्थिरांक को हटाने है workarodun यह:

आपका सबसे अच्छा अस्थायी ठीक शायद एक दूसरे अधिभार है? – PlasmaHH

+0

हां। Int arr [] को कॉन्स्ट को जोड़ने या जोड़ने के लिए आस-पास का कार्य है। लेकिन, यह जानना चाहेंगे कि यह संकलक में बग है या मेरी समझ गलत है। – Jagannath

+2

संभावनाएं हैं अगर क्लैंग, जीसीसी, आओऔ और एमएसवीसी के नवीनतम संस्करण सहमत हैं, तो यह सनसीसी में एक बग है। यह भी ध्यान रखें कि सनसीसी (इन) बग्गी होने के लिए प्रसिद्ध है। जो सवाल की ओर जाता है: जीसीसी का उपयोग क्यों नहीं करते? – PlasmaHH

उत्तर

2

संकलक पर

आउटपुट इस मामले में मानक का पालन नहीं कर रहा है और त्रुटिपूर्ण है। आइए प्रासंगिक खंडों की समीक्षा करें।

13,3/3 से पहले हमने:

...

— First, a subset of the candidate functions—those that have the proper number of arguments and meet certain other conditions—is selected to form a set of viable functions (13.3.2).

— Then the best viable function is selected based on the implicit conversion sequences (13.3.3.1) needed to match each argument to the corresponding parameter of each viable function.

तो दोनों कार्यों तर्कों की एक ही नंबर है और उम्मीदवारों माना जाता है। अब हम

13.3.3 में सबसे अच्छा व्यवहार्य समारोह को खोजने के लिए, है: तो फिर हम

Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then

— for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2), or, if not that,

— F1 is a nontemplate function and F2 is a template function specialization, or, if not that,

— F1 and F2 are template functions, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in 14.5.5.2, or, if not that,

है दो कार्यों पहला नियम (स्थिरांक जोड़ने) के लिए बराबर हैं

let ICSi(F) denote the implicit conversion sequence that converts the ith argument in the list to the type of the ith parameter of viable function F. 13.3.3.1 defines the implicit conversion sequences and 13.3.3.2 defines what it means for one implicit conversion sequence to be a better conversion sequence or worse conversion sequence than another

, और दूसरा नियम लागू नहीं होता है (दोनों टेम्पलेट्स हैं)। तो हम तीसरे नियम में चले जाते हैं। 14.5.5.2 से (जिसे मैं अनुरोध करता हूं तो उद्धरण दूंगा) हम सीखते हैं कि const int फ़ंक्शन का संस्करण const Item संस्करण से अधिक विशिष्ट है, और इसलिए सबसे अच्छा मिलान const int ओवरलोड है, जिसे तब कॉल किया जाना चाहिए।

template<size_t SZ> 
void LoopThrough(int (&Item)[SZ]) 
{ 
    LoopThrough(static_cast<const int (&)[SZ]>(Item)); 
} 
1

आपका कंपाइलर छोटी गाड़ी है। दोनों ओवरलोड में उनके टेम्पलेट तर्क कम हो जाते हैं, और ओवरलोड रिज़ॉल्यूशन को सबसे विशिष्ट चुनना चाहिए। तो एक नया कंपाइलर प्राप्त करने के अलावा, आप क्या कर सकते हैं?

सबसे पहले, यह समझने में मददगार है कि कंपाइलिंग कंपाइलर्स के साथ- आम तौर पर अलग-अलग फ़ंक्शन टेम्पलेट ओवरलोड होने का विचार नहीं है। उदाहरण देखें Herb Sutter और Andrei Alexandrescu द्वारा C++ Coding Standards: 101 Rules, Guidelines, and Best Practices का आइटम 66।

सौभाग्य से, वह आइटम भी एक संभावित फिक्स का सुझाव देता है। आपको बस एक ही फ़ंक्शन टेम्पलेट को परिभाषित करना है और उस फ़ंक्शन टेम्पलेट को क्लास टेम्पलेट फ़ंक्शन ऑब्जेक्ट में काम को प्रतिनिधि बनाना है। इसके बाद आप ints के लिए आंशिक रूप से इस कक्षा टेम्पलेट को विशेषज्ञ बना सकते हैं।

#include <iostream> 

#define PRINT_TRACE(STR) \ 
std::cout << __FILE__ << ":" << __LINE__ << ":" << STR << "\n"; 

namespace detail {  

// primary template 
template<typename Type, size_t SZ> 
class LoopThroughHelper 
{ 
public: 
    void operator()(const Type(&Item)[SZ]) 
    { 
     PRINT_TRACE("Generic version");   
    } 
}; 

// partial specialization for int arrays 
template<size_t SZ> 
class LoopThroughHelper<int, SZ> 
{ 
public: 
    void operator()(const int(&Item)[SZ]) 
    { 
     PRINT_TRACE("Specialized version"); 
     for (size_t index = 0; index < SZ; ++index) 
     { 
      std::cout << Item[index] << "\n"; 
     } 
    } 
}; 

} // namespace detail 

// one function template to rule them all 
template<typename Type, size_t SZ> 
void LoopThrough(const Type(&Item)[SZ]) 
{ 
    detail::LoopThroughHelper<Type, SZ>()(Item);   
} 

int main() 
{ 
    { 
     int arr[] = { 1, 2 }; 
     LoopThrough(arr);  
    } 
} 

अधिकतर, कंपाइलर फ़ंक्शन ऑब्जेक्ट को कॉल इनलाइन करेगा और अस्थायी रूप से पूरी तरह ऑप्टिमाइज़ करेगा। उम्मीद है कि आपका कंपाइलर भी कक्षा टेम्पलेट्स के आंशिक विशेषज्ञता को सही ढंग से कार्यान्वित करेगा। Ideone

+0

ओपी की समस्या विशेष रूप से केवल एक टेम्पलेट के साथ संकलित करने में विफल रहता है क्योंकि संकलक एक गैर-कॉन्स्ट सरणी को कॉन्स्ट सरणी पैरामीटर से बांध नहीं पाएगा। –

+0

@MarkB Regardless, यह एकाधिक फ़ंक्शन ओवरलोड के बजाय विशेष फ़ंक्शन ऑब्जेक्ट्स के लिए अतिरिक्त संकेत देने के लिए बस अच्छी शैली है। – TemplateRex