【发布时间】:2021-05-05 08:13:42
【问题描述】:
谁能解释为什么A::f(const B& b) 和f(const A::B& b) 之间存在歧义。我认为代码中的意图非常明确。
#include <iostream>
namespace A
{
class B
{
protected:
double value_;
public:
B() : value_(15.0) {}
double getValue() const {return value_;}
};
void f(const B& b)
{
std::cout << "f(b) = " << b.getValue() << std::endl;
}
}
void f(const A::B& b)
{
std::cout << "Other f(b) = " << b.getValue() << std::endl;
}
int main()
{
A::B b;
A::f(b);
f(b);
return 0;
}
但是,g++ 7.5.0 和 clang 6.0.0 都抱怨函数调用不明确
(error: call of overloaded ‘f(A::B&)’ is ambiguous) 无论编译器标志和优化如何。
【问题讨论】:
-
又一天,ADL 的另一个受害者
标签: c++ namespaces function-call argument-dependent-lookup