Kanony
Kanony

Reputation: 539

Why calling the function isn't ambiguous in SFINAE?

I am using SFINAE to check whether some class has 'print()' function or not. The code works but why calling has_print() is not ambiguous?

class cls {
public:
    void print() {
        std::cout << "some text" << std::endl;
    }
};

template<typename T>
auto has_print(T tt) -> decltype(T().print(), std::true_type()) {
    tt.print();
    return std::true_type();
}

std::false_type has_print(...) {
    std::cout << "Doesn't contain print()" << std::endl;
    return std::false_type();
}

int main() {
    cls c;
    has_print(c);

    return 0;
}

It could match both of them.

Upvotes: 2

Views: 58

Answers (1)

songyuanyao
songyuanyao

Reputation: 172964

It could match both of them.

In overload resolution, the 1st overload wins the 2nd one which takes ellipsis parameter.

  1. A standard conversion sequence is always better than a user-defined conversion sequence or an ellipsis conversion sequence.

Upvotes: 3

Related Questions