मैंने यह कोड देखने के लिए यह कोड लिखा है कि कक्षा के प्रकार में begin
फ़ंक्शन है।इस मामले में ओवरलोड रिज़ॉल्यूशन क्यों अस्पष्ट है?
struct foo //a simple type to check
{
int begin(){ return 0;}
};
struct Fallback
{
int begin(){ return 0;}
};
template<typename T>
struct HasfuncBegin : T,Fallback
{
typedef char one;
typedef int two;
template<typename X>
static one check(int (X::*)() = &HasfuncBegin<T>::begin);
template<typename X>
static two check(...);
enum :bool {yes = sizeof(check<T>())==1, no= !yes};
};
int main()
{
std::cout<< HasfuncBegin<foo>::yes;
return 0;
}
कौन सा पैदा करता है त्रुटि:
error: call of overloaded 'check()' is ambiguous
enum {yes = sizeof(check<T>())==1, no= !yes};
^
C:\XXX\main.cpp:24:16: note: candidate: static HasfuncBegin<T>::one HasfuncBegin<T>::check(int (X::*)()) [with X = foo; T = foo; HasfuncBegin<T>::one = char]
static one check(int (X::*)() = &HasfuncBegin<T>::begin);
^
C:\XXX\main.cpp:26:16: note: candidate: static HasfuncBegin<T>::two HasfuncBegin<T>::check(...) [with X = foo; T = foo; HasfuncBegin<T>::two = int]
static two check(...);
^
किसी को भी कृपया समझा क्यों कॉल अस्पष्ट (प्रयोग की जाने वाली भले ही हस्ताक्षर one check(int (X::*)() = &HasfuncBegin<T>::begin);
साथ पहली जांच समारोह डिफ़ॉल्ट तर्क है) है और भी कैसे मेरे कोड बनाने के लिए काम?
संपादित करें:
struct foo
{
int begin(){ return 0;}
};
struct Fallback
{
int begin(){ return 0;}
};
template<typename T, T ptr> struct dummy{};
template<typename T>
struct HasfuncBegin : T,Fallback
{
typedef char one;
typedef int two;
template<typename X>
static one check(dummy<int (X::*)(),&HasfuncBegin<X>::begin>*);
// even this won't work, so replace above statement with below commented one
// static one check(dummy<decltype(&HasfuncBegin<X>::begin),&HasfuncBegin<X>::begin>*);
template<typename X>
static two check(...);
enum {yes = sizeof(check<T>(0))==1, no= !yes};
};
तरह 'typename std :: सशर्त कुछ क्यों उपयोग नहीं :: मान, एक, दो> :: टाइप'? –
डिफ़ॉल्ट फ़ंक्शन तर्क फ़ंक्शन हस्ताक्षर का हिस्सा नहीं हैं और SFINAE द्वारा नहीं माना जाता है। – cpplearner
http://stackoverflow.com/q/257288/1639256 या http://stackoverflow.com/q/1966362/1639256 – Oktalist