【发布时间】:2017-03-03 08:14:10
【问题描述】:
考虑这个类:
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);
为什么这段代码有歧义?
【问题讨论】:
-
整数可以很容易地转换为浮点类型。虽然
func(int)是最佳匹配,但func(double)仍然是一个可行的替代方案,这使调用变得模棱两可。 -
您似乎遇到了一些涉及 const 和非 const 成员函数的棘手逻辑。 FWIW,将
func(double)更改为const成员函数可以解决问题。 -
@R Sahu:它没有解决问题:(
标签: c++ c++11 language-lawyer