mercredi 9 août 2017

What is the correct return in a function that modifies a potential r-value reference?

Conceptually and practice, what is the correct way to return the modified value of a function that potentially takes a r-value reference.

template<class Vector>
??? add_element(Vector&& v){
    v.emplace_back(1.);
    return ???(v);
}

Intuition tells me this, (because I don't loose information of the original type)

template<class Vector>
Vector&& add_element(Vector&& v){
    v.emplace_back(1.);
    return std::forward<Vector>(v);
}

but among other possibilities is

template<class Vector>
Vector& add_element(Vector&& v){
    v.emplace_back(1.);
    return v;
}

or even these ones, (based on this http://ift.tt/19kJMKF )

template<class Vector>
Vector // or typename std::decay<Vector>::type 
add_element(Vector&& v){
    v.emplace_back(1.);
    return v; // or std::forward<Vector>(v); 
}

What is the most generic way of returning the modified passed argument?

Aucun commentaire:

Enregistrer un commentaire