0

This is clearly not the same question as Overload resolution between object, rvalue reference, const reference. Please do not close this question. In the previous question an int is being passed while here an int && is being passed.

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 ?

  • Note that making your first function signature into `void afunc(int&)` **will** allow resolution. – Adrian Mole May 31 '21 at 17:44
  • Obviously. But that is not what I am asking. – Edward Diener May 31 '21 at 18:38
  • If the duplicate target doesn't address your concerns, please edit the question to clarify which part is still confusing you. – cigien May 31 '21 at 23:30
  • The [answer from Andrew Tomazos](https://stackoverflow.com/a/17961890/924299) seems to say that `int` and `int&&` are "indistinguishable", meaning the calls are "ambiguous" and that "there is no best match". How does your question differ? – showdev Jun 02 '21 at 20:18

0 Answers0