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?
&&f,&&&f, etc., with as many &’s as you like. Similarly, inapply, you could write(*f)(n),(**f)(n), etc.&&won't work; it's the logical and operator. That should have been& &f,& & &f, etc. This is just a quirk of the grammar. It's not actually useful. Yes, it's the same in C.& &triple, but it does work withreturn (******f)(n);. 2. Where in the C and C++ standards documents can I learn about such behavior? (I assume that there are certain environments in whichf"decays" to*for&f.)&f,&*&f,&*&*&finstead? I can't figure out a way to use indefinitely repeated ampersands.