Quick Q: When should you ever use functions over functors in C++?

Quick A: They behave differently, it depends on what you need.

Recently on SO:

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

Functions support distributed overriding. Functors do not. You must define all of the overloads of a Functor within itself; you can add new overloads of a function anywhere.

Functions support ADL (argument dependent lookup), permitting overloading in the argument-type associated namespace. Functors do not.

Function pointers are a kind of type-erased stateless functor that is a POD, as evidenced by how stateless lambdas convert into it. Such features (POD, stateless, type erasure) are useful.

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.