vendredi 9 janvier 2015

Selective forwarding function

The task is to create a single-argument function that forwards all types apart from one (Foo), which it converts (to Bar).


(Let us assume there exists a conversion from Foo to Bar).


Here are two implementations:



template<typename T>
T&& process(T&& t) {
return std::forward<T>(t);
}

Bar process(Foo x) {
return Bar{x};
}


And...



template <typename T, typename U>
T&& process(U&& u) {
return std::forward<T>(std::forward<U>(u));
}

template <typename T>
Bar process(Foo x) {
return Bar{x};
}


I have it on good authority (here) that the second one is preferable.


However, I can't understand the given explanation.


Could someone explain in detail?


PS In case it is relevant, the context in which the function is being used is given here.


Aucun commentaire:

Enregistrer un commentaire