Function objects usually should be small, so I donβt think that their transfer in value will differ markedly from performance (compare it with the work performed by the function in your body). If you pass by value, you can also get code analysis because the parameter by value is local to the function, and the optimizer can tell when and when data loading from the functor data element cannot be omitted.
If the functor does not have a state, then passing it as an argument does not imply any cost - the fill byte that the functor accepts does not have to have any special value (in Itanium Abi, used by GCC, at least). When using links, you always need to pass the address.
The latter ( const T& ) has a drawback that in C ++ 03 does not work for raw functions, because in C ++ 03 the program is poorly formed if you try to apply const to the type of the function (and this is the case with SFINAE). Later implementations instead ignore const when applied to function types.
The second ( T& ) has an obvious flaw that you cannot pass temporary functors.
In short, I would usually pass them on at a cost, unless I see a clear benefit in specific cases.
Johannes Schaub - litb
source share