के साथ त्रुटि क्यों संकलित करें यह gcc48 और clang32 के साथ संकलित क्यों नहीं है?enable_if
#include <type_traits>
template <int N>
struct S {
template<class T>
typename std::enable_if<N==1, int>::type
f(T t) {return 1;};
template<class T>
typename std::enable_if<N!=1, int>::type
f(T t) {return 2;};
};
int main() {
S<1> s1;
return s1.f(99);
}
जीसीसी त्रुटि:
/home/lvv/p/sto/test/t.cc:12:2: error: no type named ‘type’ in ‘struct enable_if<false, int>’
f(T t) {return 2;};
^
बजना त्रुटि:
/home/lvv/p/sto/test/t.cc:11:26: error: no type named 'type' in 'std::enable_if<false, int>'; 'enable_if' cannot be used to
disable this declaration
typename std::enable_if<N!=1, int>::type
^~~~
/home/lvv/p/sto/test/t.cc:16:7: note: in instantiation of template class 'S<1>' requested here
S<1> s1;
^
संपादित करें - समाधान
मैं चार्ल्स साल्विया से जवाब स्वीकार कर लिया है, लेकिन व्यावहारिक कारणों के लिए मैं प्रस्तावित कार्यवाही का उपयोग करने में सक्षम नहीं था (एन पर विशेषज्ञ)। मुझे अन्य कामकाज मिला जो मेरे लिए काम करता है।
typename std::enable_if<(sizeof(T),N==1), int>::type
आप यकीन है कि यह वैसे भी काम करने के लिए जा रहा है कर रहे हैं? आप अपने रिटर्न प्रकार से केवल विधियों को अधिभारित नहीं कर सकते हैं (जब तक कि 'enable_if' को भाषा निर्माण के रूप में कार्यान्वित नहीं किया जाता है, जैसा कि मैं वर्तमान में मान रहा हूं, एक साधारण टेम्पलेट क्लास)। – zneak
enable_if को बनाने के कामकाज को टी पर निर्भर क्यों है, बिल्कुल? –
समाधान मेरे लिए काम नहीं करता है! मुझे त्रुटि मिली: कॉल के लिए कोई मिलान करने वाला फ़ंक्शन नहीं ... ' –