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) << "
";
std::cout << apply(&triple, 7) << "
";
}
I'm confused about why this works. Is there an implicit conversion from functions to function pointers?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…