C++ 编译器选择函子而不是同名函数

C++ 编译器选择函子而不是同名函数,c++,function,functor,C++,Function,Functor,有人能解释一下什么规则决定编译器调用下面的functorf,而不是函数f #include <iostream> struct A { void operator()() { std::cout << "functor" << std::endl; } }; void f() { std::cout << "function" << std::endl; } int main() { A f; f();

有人能解释一下什么规则决定编译器调用下面的functor
f
,而不是函数
f

#include <iostream>

struct A {
    void operator()() { std::cout << "functor" << std::endl; }
};

void f() { std::cout << "function" << std::endl; }

int main()  
{
    A f;
    f();  // Output: functor
}
#包括
结构A{

void operator(){std::cout这是由于名称隐藏。当您声明
f
变量时,它隐藏了
f
函数。在该范围内使用名称
f
将引用局部变量,而不是函数

如果要调用函数
f
,则可以使用:

#包括
结构A{
void运算符(){std::cout
#include <iostream>

struct A {
    void operator()() { std::cout << "functor" << std::endl; }
};

void f() { std::cout << "function" << std::endl; }

int main()  
{
    A f;
    ::f();  // Output: function
}