दो मामलों रहे हैं जहां 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 प्रश्नों के विस्तृत उत्तर नहीं देता है। विस्तृत विस्तार की बहुत सराहना की है!
तुम जानते हो क्यों' टेम्पलेट वर्ग उदाहरण है। बहुत बहुत धन्यवाद! –
codekiddy
'उदाहरण' एक * सरल-टेम्पलेट-आईडी * के रूप में योग्यता प्राप्त करता है। –
धन्यवाद, मैंने जीसीसी के साथ कुछ परीक्षण किए हैं और ऐसे मामले हैं जहां जी ++ मुद्दों को उदाहरण के लिए अपरिभाषित संदर्भ जैसे 'उदाहरण' लेकिन एमएसवीसी संकलित बस ठीक है, ऐसे मामले भी हैं जहां एमएसवीसी चेतावनी देता है लेकिन जी ++ नहीं, जाहिर है कि ये मामले हैं मानक द्वारा न तो वर्णित और न ही परिभाषित किया गया। –
codekiddy