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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With