以下是測試代碼:爲什麼在類的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