This question already has an answer here:
The notation for std::function is quite nice when compared to function pointers. However, other than that, I can't find a use case where it couldn't be replaced by pointers. So is it just syntactic sugar for function pointers?
std::function<>
gives you the possibility of encapsulating any type of callable object, which is something function pointers cannot do (although it is true that non-capturing lambdas can be converted to function pointers).To give you an idea of the kind of flexibility it allows you to achieve:
As usual, see a live example here. Among other things, this allows you to realize the Command Pattern.
std::function
is designed to represent any kind of callable object. There are plenty of callable objects that cannot be represented in any way by a function pointer.A functor:
You cannot create an instance of
foo
and store it in abool(*)(int)
function pointer.A lambda with a lambda-capture:
However, a lambda without a capture can be converted to a function pointer:
Implementation-defined callable return values:
std::bind
's return value is an implementation-defined callable object. Only how that object may be used is specified by the standard, not its type.