让我们考虑以下示例:
#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.