const最佳匹配函数与其他函数之间的歧义

Ori*_*ent 5 c++ overloading

让我们考虑以下示例:

#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;
}
Run Code Online (Sandbox Code Playgroud)

实例

它给出了一个错误:

 error: call to member function 'f' is ambiguous
Run Code Online (Sandbox Code Playgroud)

当我删除const-qualifier时,错误就不复存在了.如果我将const-qualifier 添加到第二次重载,同样会发生同样的情况f.即如果两个过载都是同等的const,那么一切都没问题.

为什么会这样?

我的编译器是clang 3.8.

Vla*_*cow 2

成员函数有隐式参数this

因此,要调用其中一个函数,f编译器需要转换this为类型const S *或转换XO.

关于所有参数的转换都不是更好。所以编译器会报错。