When should you use functions over functors in C ++?

Functors seem to be more efficient, since the compiler embeds them more easily, and they work much better with parameterization. When do you ever use a regular old function over a functor?

+7
c ++ c ++ 11
source share
1 answer

Functions support distributed overriding. Functors do not. You must define all Functor overloads within yourself; You can add new function overloads anywhere.

Functions support ADL (argument-dependent search), allowing overloading in the namespace associated with the argument. Functors do not.

Function pointers are a (type) functional without storing a type that is a POD, as evidenced by the conversion of inactive lambdas into it. Such functions (POD, stateless, type erasure) are useful.

+15
source share

All Articles