दूसरे शब्दों में, क्यों यह ठीक संकलित करता है:सी ++ में, क्यों नहीं यह संभव दोस्त के लिए एक टेम्पलेट वर्ग के सदस्य अन्य वर्ग की टेम्पलेट प्रकार का उपयोग समारोह है?
template<typename Type>
class A{
public:
void f();
};
class B{
friend void A<int>::f();
};
template<>
void A<int>::f(){
B* var = new B();
}
हालांकि यह नहीं करता है:
template<typename Type>
class A{
public:
void f();
};
template<typename Type> // B is now a templated class
class B{
friend void A<Type>::f(); // Friending is done using B templated type
};
template<>
void A<int>::f(){
B<int>* var = new B<int>(); // var is now declared using int as its templated type
}
दूसरी कोड का टुकड़ा, संकलक (जीसीसी 6.2, कोई विशेष झंडे) के लिए कहते हैं:
main.cpp: In instantiation of ‘class B<int>’:
main.cpp:14:28: required from here
main.cpp:9:15: error: prototype for ‘void A<int>::f()’ does not match any in class ‘A<int>’
friend void A<Type>::f();
^~~~~~~
main.cpp:13:6: error: candidate is: void A<Type>::f() [with Type = int]
void A<int>::f(){
जैसा कि मैं इसे समझता हूं, दूसरे कोड स्निपेट में, जब संकलक var घोषित करता है तो बी वर्ग घोषणा को पार्स करना चाहिए, दोस्त में इस्तेमाल किए गए प्रकार को प्रतिस्थापित करें int द्वारा घोषणा, और सबकुछ ठीक काम करना चाहिए। मैं क्या खो रहा हूँ?
संपादित करें: नीचे टिप्पणी ने बताया है कि दूसरा कोड स्निपेट विशेषज्ञता के बिना बजना और विजुअल C++ 2015
बस FYI। यह बजना साथ में संकलित करने के लिए लगता है – Hayt
यह भी विजुअल C++ 2015 के साथ संकलित रूप में आप यहाँ देख सकते हैं: http://webcompiler.cloudapp.net/ – Tunichtgut
दोनों बजना 3.8 और जीसीसी 6.1.0 [डेमो] (http के लिए काम करता है: // coliru.stacked-crooked.com/a/14434a24d4688733) – Jarod42