मैं कुछ इनपुट चेकर का निर्माण कर रहा हूं जिसमें पूर्णांक और/या डबल के लिए विशिष्ट फ़ंक्शन होना आवश्यक है (उदाहरण के लिए 'isPrime' केवल पूर्णांक के लिए उपलब्ध होना चाहिए)।std :: enable_if: पैरामीटर बनाम टेम्पलेट पैरामीटर
अगर मैं enable_if
एक पैरामीटर के रूप उपयोग कर रहा हूँ यह पूरी तरह काम कर रहा है:
template <class T>
class check
{
public:
template< class U = T>
inline static U readVal(typename std::enable_if<std::is_same<U, int>::value >::type* = 0)
{
return BuffCheck.getInt();
}
template< class U = T>
inline static U readVal(typename std::enable_if<std::is_same<U, double>::value >::type* = 0)
{
return BuffCheck.getDouble();
}
};
लेकिन अगर मैं एक टेम्पलेट परमाटर के रूप में यह उपयोग कर रहा हूँ (के रूप में http://en.cppreference.com/w/cpp/types/enable_if पर प्रदर्शन किया)
template <class T>
class check
{
public:
template< class U = T, class = typename std::enable_if<std::is_same<U, int>::value>::type >
inline static U readVal()
{
return BuffCheck.getInt();
}
template< class U = T, class = typename std::enable_if<std::is_same<U, double>::value>::type >
inline static U readVal()
{
return BuffCheck.getDouble();
}
};
तो मैं निम्न त्रुटि है:
error: ‘template<class T> template<class U, class> static U check::readVal()’ cannot be overloaded
error: with ‘template<class T> template<class U, class> static U check::readVal()’
मैं ओ नहीं समझ सकता दूसरे संस्करण में क्या गलत है।
संभवतः अप्रासंगिक लेकिन VS2010 में मैं नहीं कर सकता है कि क्योंकि डिफ़ॉल्ट टेम्पलेट तर्क केवल वर्ग टेम्पलेट्स के लिए अनुमति दी जाती है - मैं जी ++ – David
के बारे में पता नहीं है यह पंडिताऊ है, लेकिन 'inline' कीवर्ड पर एक सदस्य विधि या टेम्पलेट की आवश्यकता नहीं है और निश्चित रूप से कोई सदस्य नहीं है जो टेम्पलेट भी है ;-) – AJG85