Good day. Suppose there is a try-block, where a local object is passed by value (I know that this is bad)
#include <iostream> using namespace std; class CL { public: CL() { cout<<"CL()"<<endl; } CL(CL&) { cout<<"CL(CL&)"<<endl; } ~CL() { cout<<"~CL()"<<endl; } }; int main() { try { CL cl; throw cl; } catch (CL clcop) { cout<<"cl catched"<<endl; } }
In what order should the copy constructor for clcop and the destructor for cl be called here? At first, as I understand it, the local cl is copied into a temporary throw'a object, but then different compilers have differences. VS2008 calls the copy constructor for clcop, and then the destructor for cl, devcpp calls the destructor first, and then copies it. How should it really be? PS did not throw through an explicit CL () call, since then, due to copy optimization, it does not occur