以下是测试代码:为什么在类的const版本上选择非const版本?
struct A
{
operator int();
operator int() const;
};
void foo (const int);
现在,当调用:
foo(A()); // calls A::operator int()
为什么它always chooses the non-const version?即使制作operator const int() const;
对调用foo()
也没有任何影响。除了标准参考之外,有人可以从逻辑上解释它背后的原因吗?
即使'A'是一个正常的堆叠/堆上分配对象(未暂时的),它给出了相同的结果。 – iammilind
如果该对象不是const限定的,则非重载过载在重载解析期间是更好的匹配。有没有一个理由你会认为这不是这种情况? –
为什么我会感到困惑的是,即使'foo()'接收到'const int'并且我们有一个'A :: operator const int()const'',它仍然会选择正常的'A :: operator int' 。 – iammilind