0

I have this situation:

struct A {};

void foo(A& a) {};
void boo(const A& a) {};

void foo(int& a) {};
void boo(const int& a) {};

int main() {
    foo(A()); //ok... but why?
    boo(A()); //ok

    foo(5); //error
    boo(5); //ok

    return 0;
}

Why foo(A()) works while foo(5) does not? Wouldn't it be the same situation? Is A() on foo(A()) an rvalue? Can I consider A() in this situation a temporary object?

João Paulo
  • 6,300
  • 4
  • 51
  • 80
  • `foo(A());` Should not work. What compiler are you using? – Quimby May 15 '19 at 12:18
  • I'm using Visual Studio 2015 (v140). – João Paulo May 15 '19 at 12:19
  • 1
    Visual Studio accepts this code, but not gcc. You should consider adding `const` for better portability. – Gilles-Philippe Paillé May 15 '19 at 12:19
  • 3
    remove *permissive* extension. – Jarod42 May 15 '19 at 12:19
  • the /permissive- is just for VS 2017+? Right? – João Paulo May 15 '19 at 12:26
  • 1
    MSVC has for a long time incorrectly allowed that, and hasn't fixed it entirely in order to avoid breaking older code, but there are now settings to switch it to the correct behavior. – aschepler May 15 '19 at 12:26
  • 1
    @JoãoPaulo It looks like yes, `/permissive-` is only known by 2017 and later. But https://learn.microsoft.com/en-us/cpp/build/reference/zc-referencebinding-enforce-reference-binding-rules?view=vs-2015 doesn't mention any versions, so maybe manually adding `/Zc:referenceBinding` to the compile command line would work. – aschepler May 15 '19 at 12:35

0 Answers0