问题
I have a function that accepts as an argument a function pointer. Surprisingly, I can pass in both a function pointer and an ordinary function:
#include <iostream>
#include <functional>
int triple(int a) {
return 3*a;
}
int apply(int (*f)(int), int n) {
return f(n);
}
int main() {
std::cout << apply(triple, 7) << "\n";
std::cout << apply(&triple, 7) << "\n";
}
I'm confused about why this works. Is there an implicit conversion from functions to function pointers?
回答1:
Yes, there's function-to-pointer implicit conversion:
An lvalue of function type T can be implicitly converted to a prvalue pointer to that function. This does not apply to non-static member functions because lvalues that refer to non-static member functions do not exist.
And
A pointer to function can be initialized with an address of a non-member function or a static member function. Because of the function-to-pointer implicit conversion, the address-of operator is optional:
void f(int); void (*p1)(int) = &f; void (*p2)(int) = f; // same as &f
That means when being used in context requiring a function pointer, function (except for non-static member function) would convert to function pointer implicitly, and the usage of operator&
is optional.
来源:https://stackoverflow.com/questions/54357936/implicit-cast-from-function-to-function-pointer