我有一個名爲uniVal的類,定義了轉換爲int_64的double和string。當我嘗試比較這個類的對象爲int GCC說: 候選人是:C++運算符==和用戶定義類型轉換
operator==(int, int) <built-in>
operator==(int64_t {aka long int}, int) <built-in>
operator==(unsigned int, int) <built-in>
operator==(long unsigned int, int) <built-in>
operator==(float, int) <built-in>
operator==(double, int) <built-in>
error: ambiguous overload for ‘operator==’ (operand types are ‘uniVal’ and ‘int’)
但如果我指定投加倍要明確它編譯。是否有可能讓gcc選擇最接近其他參數類型的轉換類型?
UPD: 編寫自己的操作符==對於每種類型的組合來說,offcourse會解決這個問題,但是我希望這個類能用於所有的C++類型,所以我想只寫一堆轉換函數,編譯器完成剩下的工作。
如果你寫了你自己的'operator ==(uniVal,int)'和'operator ==(int,uniVal)',那應該可以解決問題 – Justin