R. Absil
R. Absil

Reputation: 243

SFINAE check failure

the following SFINAE test fail to check wether something is a duck (it is a duck if there is a "void quack()" member function). I don't understand why. Any idea ?

struct Duck
{
    void quack() {}
};

struct Pig
{
    void groink() {}
};

template<class T>
auto test_duck(const T& t, int)
    -> decltype(t.quack(), bool())
{
    return true;
}

template<class T>
auto test_duck(const T& t, long)
{
    return false;
}

template<class T>
bool is_duck(const T& t)
{
    return test_duck(t, 0);
}    

int main()
{
    Duck duck;
    Pig pig;

    cout << is_duck(duck) << endl;  
    cout << is_duck(pig) << endl;
}

Upvotes: 0

Views: 72

Answers (1)

Max Langhof
Max Langhof

Reputation: 23691

The problem is that you are passing a const Duck& but Duck only has a non-const quack() method.

Edit: Looks like comments already figured it out in time. If @PiotrSkotnicki wants to post an answer (instead of answering in the comments) I'll delete this.

Upvotes: 1

Related Questions