27

Compare generic integration functions:

template <class F> double integrate(F integrand);

with

template <class F> double integrate(F& integrand);

or

template <class F> double integrate(const F& integrand);

What are the pros and cons of each? STL uses the first approach (pass by value), does it mean it's the most universal one?

3
  • STL usually uses the first approach because many problems arise when you use containers of references, etc. Nov 19, 2011 at 18:47
  • 2
    Same as everywhere else: Depending on what F is, these versions can range from being identical to very different. If F is stateful, only the middle version might be possible.
    – Kerrek SB
    Nov 19, 2011 at 18:47
  • Basically, you're putting requirements on your users whichever form you choose. If by value, you're requiring that they give you a functor that can be copied w/o losing integrity. If by reference, then you're requiring that they give you something with lifetime appropriate to whatever user you're putting it.
    – Mordachai
    Nov 19, 2011 at 19:01

3 Answers 3

34

Function objects usually should be small so I don't think that passing them by value will suffer from performance noticably (compare it to the work the function does in its body). If you pass by value, you can also gain from code analysis, because a by value parameter is local to the function and the optimizer may tell when and when not a load from a data member of the functor can be omitted.

If the functor is stateless, passing it as argument implies no cost at all - the padding byte that the functor takes doesn't have to have any particular value (in the Itanium Abi used by GCC at least). When using references, you always have to pass an address.

The last one (const T&) has the drawback that in C++03 that doesn't work for raw functions, because in C++03 the program is ill-formed if you try to apply const to a function type (and is an SFINAE case). More recent implementations instead ignore const when applied on function types.

The second one (T&) has the obvious drawback that you cannot pass temporary functors.

Long story short, I would generally pass them by value, unless I see a clear benefit in concrete cases.

8

STL uses the first approach (pass by value)

Sure, the standard libraries pass iterators and functors by value. They are assumed (rightly or wrongly) to be cheap to copy, and this means that if you write an iterator or a functor that is expensive to copy, you might have to find a way to optimize that later.

But that is just for the purposes for which the standard libraries use functors - mostly they're predicates, although there are also things like std::transform. If you're integrating a function, that suggests some kind of mathematics libraries, in which case I suppose you might be much more likely to deal with functions that carry a lot of state. You could for example have a class representing nth order polynomials, with n+1 coefficients as non-static data members.

In that case, a const reference might be better. When using such a functor in standard algorithms like transform, you might wrap it in a little class that performs indirection through a pointer, to ensure that it remains cheap to copy.

Taking a non-const reference is potentially annoying to users, since it stops them passing in temporaries.

4

Given the context, F is expected to be a "callable object" (something like a free function or a class having a operator() defined)

Now, since a free function name cannot be an L-value, the second version is not suitable for that. The third assumes F::operator() to be const (but may not be the case, if it requires to alter the state of F) The first operates on a "own copy", but requires F to be copyable.

None of the three is "universal", but the first is most likely working in the most common cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.