Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

Functors are apparently more efficient since the compiler is more easily able to inline them, and they work much better with parametrization. When should you ever use a plain old function over a functor?

like image 977
nilcit Avatar asked May 03 '17 22:05

nilcit


1 Answers

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 (kind of) a 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.

like image 127
Yakk - Adam Nevraumont Avatar answered Nov 14 '22 23:11

Yakk - Adam Nevraumont