3

I'm diving into the implicit conversions of C++20, and came up with this example:

#include <iostream>

void f(int a){ std::cout << 1; }
void f(int &a){ std::cout << 2; }

int main(){
  int a = 1;
  int &b = a;

  f(a); // This is ambiguous
  f(b); // So is this

  const int c = 1;
  f(c); // This works, as you cannot take a reference of a const
  f(42); // So does this, normal
}

Which leaves me with the following questions:

  1. How can I call the second definition of f?
  2. If I cannot call it, why doesn't the compiler tell me so?

For 2., I'm currently learning rust (since 2 years), and am used to be informed for every stupid thing I do by the compiler...

Thanks for any help,

Linus

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
ineiti
  • 324
  • 3
  • 11
  • 1
    Possibly duplicate of https://stackoverflow.com/q/38957516/580083. – Daniel Langr Dec 13 '22 at 08:52
  • @DanielLangr - absolutely correct. I searched and clicked on all proposed articles when asking this, but didn't find this one. Now I'm off searching if I can delete my question :) – ineiti Dec 13 '22 at 08:59
  • Yes, you should delete this question. Otherwise, it will likely be closed as a duplicate. – Daniel Langr Dec 13 '22 at 09:06

0 Answers0