mercredi 6 mai 2020

C++ function overloading by value type and constness (full details)

In C++, a function f taking a single argument of type T could have five different signatures (if I am not missing others):

void f(      T);   // #0
void f(      T&);  // #1
void f(const T&);  // #2
void f(      T&&); // #3
void f(const T&&); // #4

What are the full rules that the compiler follows for selecting among (a subset of) these overloads?

I would like to be able to discern in particular the cases in which:

  • f is called on a variable declared as either T, T&, const T&, T&&, const T&&;
  • f is called on the output of a function returning either T, T&, const T&, T&&, const T&&.

PS: I already found here a reasonably complete description of what happens without case #0, but I would like to learn what happens when you throw #0 into the mix.

Aucun commentaire:

Enregistrer un commentaire