3

A lot of places in the STL and elsewhere you see code like this:

template <typename Func>
void foo(Func f) 
{
      // ...
      f();
}

i.e. the callable parameter f is passed by (copy of) value. Why is this seemingly preferred over typing the parameter as Func&, const Func&, or Func&&?

8
  • 2
    Func f is a pointer to a function, which resides in the (read-only) code section of the executable image, so there's really no point in passing Func&, since: 1. the amount of data copied into the stack would be identical (size of a memory address) in both cases. 2. You would not be able to change that reference (had you passed it as reference) anyway. Dec 23, 2014 at 12:51
  • 3
    Func f is not necessarily a pointer to a function. It could be a functor as well. Also, Func && f is much better than Func f, in general. Dec 23, 2014 at 13:00
  • @Nawaz: Func&& f is only better than Func f if f is quite obviously forwarded and entity using Func&& is clear about passing f on (obviously using std::forward<Func>(f) in some form). That is the case for function adaptors but is not the case, e.g., for algorithms. Dec 23, 2014 at 14:28
  • @DietmarKühl: Is it worse in the case when it isn't better? Dec 23, 2014 at 14:57
  • @Nawaz: yes. When passing a T&& in a context you'll get a reference in all cases and you can't copy the object because you can't assume that the deduced type based on the static type of the argument matches the actually used dynamic type. You can make this assumption when taking arguments by value, though. Also, the compiler can't make the assumption that the object is local and may need to follow the indirection (I don't have benchmarks showing this effect, though: I haven't tried; the semantic argument should be sufficient...). Dec 23, 2014 at 15:04

1 Answer 1

1

The simple reason is that you can turn a value of a deduced type to use reference semantics, e.g., by passing std::ref(x) instead of x to the function. Obviously, for things which have more interesting operations than a function call operator you may need to use a custom wrapper but in general it is straight forward to write a wrapper for a class which give reference semantics to a value type.

On the other hand, once you are in a reference world, there is no way to undo this referencing under the control of the caller. Any reasonable generic function which receives a reference for an object cannot assume that the object behind this reference actually has exactly the referenced type but rather has to assume that the objects would get sliced to their base class. Thus, it can't just copy an argument received, for example, as Func& to a Func by copying it.

Taking these two realizations together pass by value for deduced types is the obvious choice for entities which may be copied in the implementation of the generic function. Note that this reasoning applies generally to arguments passed to generic functions not just to callable arguments.

2
  • isn't object slicing an assumed risk anywhere you accept a reference parameter? why would this particular case (callable objects) prefer pass by value then? Dec 23, 2014 at 15:33
  • @sdzivanovich: sure it is a general issue: once you have a reference you can't copy it anymore. However, if you have a deduced value argument you can give it reference senantics (see my answer). Dec 23, 2014 at 15:37

Your Answer

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

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