vendredi 16 décembre 2022

Advantage in writing two overloads for "T const&" and "T&&" rather than just the one taking a const ref (when we never want to move)?

Suppose I need to write a function which might take either a constant reference to an lvalue or a temporary value, is there any advantage in terms of performance in writing both overloads, the one taking a const T& and one taking T&&, if we do not want to move from the rvalue?

I was assuming having two overloads (or just writing the function once with universal references) would be beneficial but I can't pin down the exact reason. I even tried a small example: https://godbolt.org/z/53r34x4Mj but I can't really make sense of the generated code.

Aucun commentaire:

Enregistrer un commentaire