In Scott Meyer's book Effective Modern C++ on page 167 (of the print version), he gives the following example:
auto timeFuncInvocation = [](auto&& func, auto&&... params) {
// start timer;
std::forward<decltype(func)>(func)(
std::forward<decltype(params)>(params)...
);
// stop timer and record elapsed time;
};
I completely understand the perfect forwarding of params
, but it is unclear to me when perfect forwarding of func
would ever be relevant. In other words, what are the advantages of the above over the following:
auto timeFuncInvocation = [](auto&& func, auto&&... params) {
// start timer;
func(
std::forward<decltype(params)>(params)...
);
// stop timer and record elapsed time;
};
See Question&Answers more detail:os