के साथ विफल रहता है मैं समझने की कोशिश कर रहा हूं कि सी ++ (11) <type_traits>
का उपयोग कैसे करें।टेम्पलेट तर्क प्रकार कटौती सी ++ 11 <type_traits>
यहाँ मेरी तुच्छ परीक्षण कार्यक्रम
#include <type_traits>
template<class U, class S>
inline U add(typename std::enable_if<std::is_unsigned<U>::value,U>::type a,
typename std::enable_if<std::is_signed <S>::value,S>::type b)
{
return a + b;
}
int main(int argc, const char * argv[], const char * envp[])
{
unsigned int ui;
int i;
auto a = add(ui, i);
return 0;
}
है जब मैं कोई सुराग नहीं क्यों जीसीसी टेम्पलेट पैरामीटर U
अनुमान नहीं कर सकता है के रूप में
/home/per/f.cpp: In function ‘int main(int, const char**, const char**)’:
/home/per/f.cpp:15:23: error: no matching function for call to ‘add(unsigned int&, int&)’
auto a = add(ui, i);
^
/home/per/f.cpp:15:23: note: candidate is:
/home/per/f.cpp:5:10: note: template<class U, class S> U add(typename std::enable_if<std::is_unsigned<U>::value, U>::type, typename std::enable_if<std::is_signed<S>::value, S>::type)
inline U add(typename std::enable_if<std::is_unsigned<U>::value,U>::type a,
^
/home/per/f.cpp:5:10: note: template argument deduction/substitution failed:
/home/per/f.cpp:15:23: note: couldn't deduce template parameter ‘U’
auto a = add(ui, i);
^
जीसीसी 4.8.1 यह त्रुटियों के साथ संकलित। कोई भी जानता है कि मेरे कोड में कौन सी जानकारी गुम है, इस तरह मैं सी ++ 11 में एक प्रोग्राम लिखता हूं जो पहले हस्ताक्षर के रूप में एक हस्ताक्षरित अभिन्न प्रकार लेता है और दूसरे के रूप में हस्ताक्षर किए गए अभिन्न प्रकार को लेता है?
आप decuce नहीं कर सकते हैं प्रकार के एक योग्यता के 'छोड़ ::'। –
** 14.8.2.5 किसी प्रकार से टेम्पलेट तर्कों को कम करना [temp.deduct.type] ** 5 गैर-कटौती वाले संदर्भ हैं: - एक प्रकार का नेस्टेड-नाम-विशिष्टता जो क्वालिफ़ी-आईडी का उपयोग करके विशिष्ट थी। – TemplateRex
सभी अच्छी टिप्पणियों और उत्तरों के लिए धन्यवाद! –