我有一个名为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