mercredi 30 septembre 2015

Ambiguous call in custom member detector

I was working in my own implementation of a member detector to improve my programming skills. The following code compile well with g++ but clang++ reject the code, the error is :

error: call to 'check' is ambiguous main.cpp:19:17: note: candidate function [with U = has_member::HasIt]

static char check( decltype(U::i)* );

main.cpp:22:16: note: candidate function [with U = has_member::HasIt]

static int check(U*);

Here's the code of the class

template<typename T>
struct has_member 
{

    struct Fallback
    {
        int i;
    };

    struct HasIt : Fallback, T
    {};

    template<class U>
    static char check( decltype(U::i)* ); 

    template<typename U>
    static int check(U*);    

    static const bool value = sizeof(check<HasIt>( nullptr ) ) == sizeof(int);
};

class Test
{
public:

};

int main()
{
    auto v  = has_member<Test>::value;

    std::cout << std::boolalpha << v;
}

Live example here

The question is : is the code valid ? If it is why g++ accepts it ?

Aucun commentaire:

Enregistrer un commentaire