lundi 31 mai 2021

Choosing overload between a type and an rvalue reference of that type

I thought I understood rvalue references but evidently I am missing something when it comes to function overloading. Given the code:

#include <iostream>
#include <utility>

void afunc(int)
    {
    std::cout << "\nafunc(int)\n";
    }
    
void afunc(int &&)
    {
    std::cout << "\nafunc(int &&)\n";
    }
    
int main()
  {
  
  int i = 0;
  
  afunc(std::move(i));
  }

I would have assumed that the second call to afunc would be chosen, but instead I get that the error that the call to afunc is ambiguous. Since std::move(i), as I understand it, creates an rvalue reference to i, an int, I do not understand why the second call to afunc does not match exactly. Can someone please explain what I am understanding that is wrong ?

This is clearly not the same question as Overload resolution between object, rvalue reference, const reference. In the previous question an int is being passed while here an int && is being passed, which should match exactly only one of the function calls.

Aucun commentaire:

Enregistrer un commentaire