如何在第二个 f()
中进行调用,通过非限定查找找到第一个 f()
,以便 ADL 和普通名称查找规则仍然有效?
注意:我不想更改任何函数的名称。
namespace x
{
void f()
{
}
class C
{
void f()
{
x::f();
}
};
}
最佳答案
只需引入函数:
void f()
{
using x::f;
f(); // calls x::f()
}
这样做的原因是,复制 Mike Seymour's answer :
Because the using declaration brings x::f into the scope of f, which is narrower than that of C. Unqualified lookup considers the local block scope, finds a match, and stops before considering the wider class scope. There is no argument-dependent lookup since there are no function arguments, so no further scopes are considered.
关于c++ - 如何在仍然允许 ADL 等的情况下调用同名的其他函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27315952/