Both forms are equivalent. I prefer a form that explicitly shows that the parameter is a pointer. Knowing that a parameter is a pointer is important because you can pass NULL , nullptr or 0 as an argument. Your program will compile, but crashes if someone makes a call to f(0) . You always want to check if a function pointer is not a null pointer before calling pointee if you are not sure that you cannot call your function with NULL , nullptr or 0 arguments.
If you use lambdas in your project, you should use `templates. Otherwise, you can continue to use raw function pointers, but make sure you check the function pointer (if necessary)
template <typename Function> int f(const Function& functionp) { if(functionp) functionp(); return 20; }
Lambdas and std::function<> objects also have a bool operator, so the if(functionp) line will also work for them. It will evaluate to false for std::function<> objects that contain nullptr, and otherwise, it will evaluate to true for std::function<> and lambdas objects.
source share