jeudi 17 mars 2016

Return forwarding reference parameters - Best practice

In the following scenario

template <class T, class U>
? f(T&& a, U&& b)
{
    return a > b ? a : b; 
}

what would the optimum return type be ? My thoughts so far are :

  1. Return by r value refs, perfectly forwarding the function arguments :

    template <class T, class U>
    decltype(auto) f(T&& a, U&& b)
    {
        return a > b ? forward<T>(a) : forward<U>(b); 
    }
    
    
  2. Move construct the return value :

    template <class T, class U>
    auto f(T&& a, U&& b)
    {
        return a > b ? forward<T>(a) : forward<U>(b); 
    }
    
    
  3. Try fo find a way to enable (N)RVOs (even though I think since I want to use the function arguments that's impossible)

Is there a problem with these solutions ? Is there a better one ? What's the best practice ?

Aucun commentaire:

Enregistrer un commentaire