A code snippet I saw in Effective Modern C++ has a clever implementation of the instrumentation rationale to create a function timer :
auto timeFuncInvocation =
[](auto&& func, auto&&... params)
{
start timer;
std::forward<decltype(func)>(func)(
std::forward<decltype(params)>(params)...);
stop timer and record elapsed time;
};
My question is about std::forward<decltype(func)>(func)(...
- To my understanding, we are actually casting the function to its original type, but why is this needed? It looks like a simple call would do the trick.
- Are there any other cases where we use perfect forwarding to make a function call ?
This looks like a good use case for the use of familiar template syntax in lambda expressions in case we wanted to make the timer type a compile time constant.