Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguity between const best matching function and other function

Let us consider following example:

#include <type_traits>

#if 1
struct X {};

struct O
{
    O(X) { ; }
};
#else
struct O {};

struct X
{
    operator O () { return {}; }
};
#endif

static_assert(std::is_convertible< X, O >::value);

struct S
{
    void f(X) const { ; }
    void f(O) { ; }
};

#include <cstdlib>

int
main()
{
    S s;
    s.f(X{});
    return EXIT_SUCCESS;
}

Live example

It gives an error:

 error: call to member function 'f' is ambiguous

When I removing const-qualifier, then the error cease to exist. Equally the same happens, if I add const-qualifier to the second overloading of f. I.e. if both overloadings are equally const-qualified, then all is OK.

Why is it so?

My compiler is clang 3.8.

like image 761
Tomilov Anatoliy Avatar asked Jan 15 '16 17:01

Tomilov Anatoliy


1 Answers

Member functions have implicit parameter this.

So to call one of the functions f the compiler needs either to convert this to type const S * or to convert X to O.

Neither conversion regarding all parameters is better. So the compiler issues an error.

like image 167
Vlad from Moscow Avatar answered Sep 27 '22 16:09

Vlad from Moscow