Ho scritto questo codice per verificare se un tipo di classe ha la funzione begin
.Perché la risoluzione del sovraccarico è ambigua in questo caso?
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;
}
che produce errore:
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(...);
^
Qualcuno può spiegare perché chiamata è ambigua (anche se prima funzione di controllo con la firma one check(int (X::*)() = &HasfuncBegin<T>::begin);
ha argomento di default da utilizzare) e anche il modo di fare il mio codice lavoro?
Edit:
Così qui è il codice finale di lavoro:
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};
};
Perché non usare qualcosa come 'typename std :: condizionale :: value, uno, due> :: type'? –
Gli argomenti della funzione predefinita non fanno parte della firma della funzione e non vengono considerati da SFINAE. – cpplearner
Possibile duplicato di http://stackoverflow.com/q/257288/1639256 o http://stackoverflow.com/q/1966362/1639256 – Oktalist