Consider the following toy code:
class X {};
class Y {
public:
Y() { cout << "Y ctor\n"; }
~Y() { cout << "Y dtor\n"; }
};
int gun() {
throw X{};
return 42;
}
void fun(Y yy, int i) {}
int main()
{
Y a;
cout << "--------\n";
try
{
fun(a, gun());
}
catch (const X&)
{
cout << "catched\n";
}
cout << "--------\n";
}
The outputs of this are as follows:
Y ctor
--------
catched
--------
Y dtor
May I ask why the destructor of the parameter yy (copy initialized by a) is not called?
Same outputs are produced when I switch the order of the arguments (i.e. using void fun(int i, Y yy) {} and fun(gun(), a);), so I don't think it is because of the undefined order of evaluation of function arguments.
Update: see demo