2015-12-29 17 views
5

दो मामलों रहे हैं जहां typedef मुझे confuses जब यह extern template declaration और explicit template instantiation लिए आता है।टेम्पलेट इन्स्टेन्शियशन और निर्वासन टेम्पलेट घोषणा में typedef का उपयोग कर

नीचे दो उदाहरण उदाहरण स्निपेट को देखने के लिए।

उदाहरण (केस 1) निम्नलिखित पर विचार करें:

// suppose following code in some cpp file  
template <typename T> 
    struct example 
{ 
    T value; 
}; 

// valid typedefs 
typedef example<int> int_example; 
typedef example<std::string> string_example; 

// explicit instantiation using above typedefs 
template class int_example; // -> compile time error 
template class string_example; // -> compile time error 

// instead we need to use type names 
template class example<int>; // -> OK 
template class example<std::string>; // -> OK 

// QUESTION 1: Why does this work however? is this valid code? 
typedef std::string type_string; 
template class example<type_string>; 

क्यों typedef साथ template class example<type_string> काम करता है? और यह वैध क्यों है जबकि template class string_example नहीं है?

// suppose following code is in some header file 
template <typename T> 
struct example 
{ 
    T value; 
}; 

// valid typedefs 
typedef std::string type_string; 
typedef example<type_string> string_example; 

// Explicit instantiation declaration 
// QUESTION 2: Is this valid code? if not why not? 
extern template string_example; // -> at least this compiles, but is it OK? 

ऊपर टिप्पणी में पूछताछ की है, यह extern template declaration में typedef उपयोग करने के लिए, उपरोक्त उदाहरण की तरह वैध है, और यही कारण है कि इस के विपरीत संकलित करता है:

उदाहरण (केस 2) निम्नलिखित पर विचार करें केस 1 जहां यह नहीं करता है।

मैंने इसी तरह के मामलों के बारे में पढ़ा है लेकिन कोई भी 2 प्रश्नों के विस्तृत उत्तर नहीं देता है। विस्तृत विस्तार की बहुत सराहना की है!

उत्तर

2
template class int_example; 

कानूनी नहीं है। सी ++ 11 stanard से:

14.7.2 स्पष्ट इन्स्टेन्शियशन

2 स्पष्ट इन्स्टेन्शियशन के लिए वाक्य रचना है:

स्पष्ट-इन्स्टेन्शियशन:
extern ऑप्टtemplateघोषणा

स्पष्ट तत्कालता के दो रूप हैं: एक स्पष्ट तत्काल परिभाषा और एक स्पष्ट तत्काल घोषणा। एक स्पष्ट तत्काल घोषणा extern कीवर्ड से शुरू होती है।

3 स्पष्ट इन्स्टेन्शियशन कक्षा या सदस्य वर्ग के लिए है, तो सविस्तार-प्रकार-विनिर्देशकघोषणा में एक सरल-टेम्पलेट आईडी में शामिल होगा।

सरल-टेम्पलेट आईडी धारा ए में परिभाषित किया गया है12 टेम्पलेट्स के रूप में:

सरल-टेम्पलेट आईडी:
टेम्पलेट नाम<टेम्पलेट तर्क-सूची ऑप्ट >

int_example एक के रूप में योग्य नहीं है सरल-टेम्पलेट-आईडी
example<int>सरल-टेम्पलेट-आईडी के रूप में योग्यता प्राप्त करता है।

हालांकि, उस तर्क के आधार पर

extern template string_example; 

नहीं कानूनी या तो है। मुझे नहीं पता कि यह आपके लिए कैसे काम करता है। जब मैंने g ++ 4.9.3 में ऐसी लाइन को संकलित करने का प्रयास किया तो मुझे निम्न त्रुटि मिली। `और` निर्वासन टेम्पलेट वर्ग उदाहरण ; `काम करता है (या यह जीसीसी के साथ नहीं है), मैं btw MSVC-140 उपयोग कर रहा हूँ और यह काम करता

socc.cc:15:31: error: expected unqualified-id before ‘;’ token 
extern template string_example; // -> compile time error 
+0

तुम जानते हो क्यों' टेम्पलेट वर्ग उदाहरण है। बहुत बहुत धन्यवाद! – codekiddy

+0

'उदाहरण ' एक * सरल-टेम्पलेट-आईडी * के रूप में योग्यता प्राप्त करता है। –

+0

धन्यवाद, मैंने जीसीसी के साथ कुछ परीक्षण किए हैं और ऐसे मामले हैं जहां जी ++ मुद्दों को उदाहरण के लिए अपरिभाषित संदर्भ जैसे 'उदाहरण ' लेकिन एमएसवीसी संकलित बस ठीक है, ऐसे मामले भी हैं जहां एमएसवीसी चेतावनी देता है लेकिन जी ++ नहीं, जाहिर है कि ये मामले हैं मानक द्वारा न तो वर्णित और न ही परिभाषित किया गया। – codekiddy

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