dimanche 4 septembre 2016

Forwarding the same value to two or more functions

When using forwarding references, is it a bad idea to forward the same value to more than one functions? Consider the following piece of code:

template<typename Iter>
constexpr Iter
select(Iter start, Iter end)
{ ... }

template<typename Container>
constexpr decltype(auto)
operator()(Container&& c)
{
    return *select(std::begin(std::forward<Container>(c)),
                   std::end(std::forward<Container>(c)));
}

If Container is an lvalue-reference, the function works just fine. However, I'm worrying about situations where rvalues are passed on to it, because the value would get invalided once a move operation occurs. My doubt is: Is there a correct way to forward the container in that case, without losing the value category?

Aucun commentaire:

Enregistrer un commentaire