एक बड़े कार्यक्रम के लिए एक विशेषता वर्ग के हिस्से के रूप में, मैंने एक स्थैतिक वर्ग चर बनाने का प्रयास किया, जिसमें संलग्न वर्ग टेम्पलेट को तत्काल के प्रकार के आधार पर एक अलग मूल्य हो सकता था।जी ++ कंपाइलर त्रुटि या दोषपूर्ण कोड? : "गैर-टेम्पलेट की टेम्पलेट परिभाषा"
मैं प्रासंगिक कोड सरल बना दिया है मैं किस बारे में बात कर रहा हूँ के एक नंगे हड्डियों उदाहरण उत्पादन करने के लिए:
#include <iostream>
#include <string>
#include <type_traits>
template <class T, class Enable = void>
struct Foo;
template <class T>
struct Foo<T,
typename std::enable_if<std::is_integral<T>::value>::type
>
{
static std::string message;
};
template <class T>
struct Foo<T,
typename std::enable_if<std::is_floating_point<T>::value>::type
>
{
static std::string message;
};
template <class T, class Enable>
std::string Foo<T, Enable>::message;
जीसीसी 4.6 के साथ
, यह एक संकलक त्रुटि देता है: template definition of non-template ‘std::string Foo<T, Enable>::message
। समस्या पिछले दो पंक्तियों के कारण होती है, जहां मैं केवल स्थिर चर std::string Foo<T, Enable>::message
को परिभाषित कर रहा हूं।
मुझे उलझन में है कि यह क्यों हो रहा है। यदि मैं पिछले दो पंक्तियों को छोड़ देता हूं (लेकिन निश्चित रूप से लिंकर त्रुटियों का कारण बनता है) संकलक त्रुटि दूर हो जाती है।) क्या यह जीसीसी के साथ एक कंपाइलर त्रुटि है?
+1 "एक नंगे-हड्डियों उदाहरण" के लिए +1। http://sscce.org/। –
आपको –
@ वॉन शामिल करने की आवश्यकता है, यह समस्या नहीं है, लेकिन मैंने कोड स्निपेट अपडेट किया है। – Channel72