This program:
#include <iostream>
using namespace std;
struct B {
B() { cout << "B"; }
//B(const B& b) { cout << "copyB"; }
~B() { cout << "~B"; }
};
struct C : B {
};
void f(B b) {
}
int main() {
C c;
f(c);
return 0;
}
outputs B~B~B~B, i.e. three times calling destructor, why?
Only in MSVC. Clang and GCC outputs B~B~B (which is most likely correct).
And interesting thing: if you uncomment copy-ctor, it outputs BcopyB~B~B, which is correct (destructor called two times).
Is it a bug in MSVC compiler? Or it is correct behavior?
(Visual Studio 2019 latest, cl.exe version 19.28.29337)