In which cases, when is it important to call std::forward<L>(lambda) as in the following 2 functions (given that lambda stands for some callable object)
1 2 3 4 5 6 7 8
template<class Tpl, class L>
void iterate_tuple(std::index_sequence<>, L&& /*lambda*/) {}
template<class Tpl, size_t I, size_t... Idx, class L>
void iterate_tuple(std::index_sequence<I, Idx...>, L&& lambda) {
lambda((std::tuple_element_t<I, Tpl>*)nullptr);
iterate_tuple<Tpl>(std::index_sequence<Idx...>{}, std::forward<L>(lambda));
}
The std::forward() is used to keep the original type of a passed object.
Otherwise it is possible that a temporary object is created and the object passed as && is copied to the temporary object and then passed as a simple reference (&).
The reason is that the compiler prefers & over &&.
A lambda object may contain a lot of data and hence it is better not to copy it.