C++ 编译器选择函子而不是同名函数
有人能解释一下什么规则决定编译器调用下面的functorC++ 编译器选择函子而不是同名函数,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();
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
}