There are several use cases that should be available:
You cannot solve both cases with one parameter of a reference type: the first case requires a reference to the constant lvalue or the reference value rvalue, which prohibits second use, and the second case requires a reference lvalue, which prohibits first use.
A common idiom in such situations is to take a functor by value and return it at the end:
template <typename Iter, typename F> F map(Iter first, Iter last, F f) {
This may not be entirely applicable in your case, but it is an idea. For example, you can return std::pair<ParticleType, F> . In any case, you need your functor type to be copied, but this is a reasonable requirement.
An alternative useful to @Xeo and available only to function templates is to accept a functor argument as a universal reference, which will work in both cases:
template <typename Iter, typename F> void map(Iter first, Iter last, F && f) {
Please note that in this case we do not use std::forward , since we use f as a genuine link, and not just pass it through to another place. In particular, we are not allowed to move from f if we are still planning on using it.
Kerrek SB
source share