c – 为什么这个对成员函数的调用是模糊的?
考虑这个类:
class Base{ public: void func(double a) = delete; void func(int a) const {} }; int main(){ Base base; base.func(1); return 0; } 当使用clang编译时,会产生以下错误: clang++ --std=c++11 test.cpp test.cpp:22:7: error: call to member function 'func' is ambiguous base.func(1); 用g,会产生警告: g++ -std=c++11 test.cpp test.cpp: In function ‘int main()’: test.cpp:22:13: warning: ISO C++ says that these are ambiguous,even though the worst conversion for the first is better than the worst conversion for the second: base.func(1); 为什么这个代码不明确? 解决方法
非静态成员函数,如下:
void func(double); // #1 void func(int) const; // #2 也接受一个implicit object parameter,被认为是超负荷决议([over.match]/p1)任何其他争论:
将隐式对象参数并入成员函数签名后,编译器会看到两个重载: void func(Base&,double); // #1 void func(const Base&,int); // #2 并尝试根据呼叫选择最佳可行功能: Base base; base.func(1); 从base(它是Base的非常量值)到Base&具有完全匹配等级(直接引用绑定产生一个Identity conversion) – 参见Table 13.从base到const Base&也是一个完全匹配的排名,但是,[over.ics.rank]/p3.2.6声明#1有一个更好的转换顺序:
现在对于第二个参数,从积分pr值1到double的转换是浮点积分转换([conv.fpint]),它被赋予转换等级.另一方面,1到int是具有完全匹配等级的身份转换.对于这个参数,#2被认为具有更好的转换顺序([over.ics.rank]/p3.2.2):
过载分辨率成功要求至多存在一个参数,转换序列不同([over.match.best]):
这里,ICS0(#1)优于ICS0(#2),反之,ICS1(#2)优于ICS1(#1),所以编译器不能在两次重载之间进行选择并检测模糊. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |