samedi 2 novembre 2019

Forwarding vs not Forwarding a function passed to a wrapper

My question is what is the advantage of perfect forwarding a function, which is passed to our wrapper.

template<typename T, typename ...U>
    auto time_function(T&& func, U&& ...args)
{
    std::cout<<"timing void function"<<std::endl;
    //std::forward<T>(func)(std::forward<U>(args)...); //this vs next one
    func(std::forward<U>(args)...);

    std::cout<<"timing over"<<std::endl;
}

In the case of argument forwarding, it is clear that forwarding preserves the lvalueness vs rvalueness of the argument. However, is there any use to forward func before calling?

Let's say I pass both temporary functors and normal functions to the time_function wrapper.

Aucun commentaire:

Enregistrer un commentaire