0

Simplest example:

struct A {};
struct B : public A {};
void Call(A&) {}
int main() { Call(B()); return 0; }

This goes through in MSVC. This doesn't go through with clang 7.0.0-3:

test.cpp:4:14: error: no matching function for call to 'Call'
int main() { Call(B()); return 0; }
             ^~~~
test.cpp:3:6: note: candidate function not viable: no known conversion from 'B' to 'A &' for 1st argument
void Call(A&) {}
     ^

Why?

I would've expected the constructor to be called, the object being kept in memory by main(), the reference being honored for the function call, and then the object being destroyed once the Call() function is executed. Instead, it simply doesn't compile.

Michel Donais
  • 474
  • 4
  • 13
  • MSVC doesn't like it either live: https://godbolt.org/z/YkM3JP – Richard Critten Jun 18 '19 at 20:44
  • 1
    See the dupe for why this works on MSVS. You need `void Call(const A&)` to be portable. – NathanOliver Jun 18 '19 at 20:44
  • In this case, I couldn't make it const& as it needs to be modified, I expect my behaviour to be executed in the destructor. It makes sense it's weird, though. – Michel Donais Jun 18 '19 at 20:48
  • @RichardCritten EDIT: Played with godbolt. It's because I am calling the compiler directly, while the instance you provided has the "/W4 /permissive-" parameters. Remove these and it goes through. – Michel Donais Jun 18 '19 at 20:48
  • 1
    Try with /permissive- see: https://learn.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=vs-2019 – Richard Critten Jun 18 '19 at 20:53
  • Exactly. Thanks. – Michel Donais Jun 18 '19 at 20:53
  • 1
    Unless you are trying to work with MS extensions I think those 2 switches should normally be required. – Richard Critten Jun 18 '19 at 20:55
  • Agreed! Well, what I worked on has it. And building with the command prompt without argument has it. So for me, it was a clang vs msvc. That said, the end result is pretty much this is not allowed because Stoustrup saw it as evil, not because this is technically invalid code. There's actual exception in code to make it not work. – Michel Donais Jun 18 '19 at 21:00

0 Answers0