|
From: Bojan N. <bo...@bn...> - 2011-04-10 13:51:32
|
Luigi Ballabio <lui...@gm...> writes: > the compiler (well, gcc at least) will complain that the copy > constructor is private at the "throw" line. All compilers should do the same: ,---- From http://www.open-std.org/jtc1/sc22/open/n2356/except.html | When | the thrown object is a class object, and the copy constructor used to | initialize the temporary copy is not accessible, the program is ill- | formed (even when the temporary object could otherwise be eliminated). | Similarly, if the destructor for that object is not accessible, the | program is ill-formed (even when the temporary object could otherwise | be eliminated). | `---- However, this is not the same as saying that the copy constructor is actually always invoked because of the following statement ,---- From http://www.open-std.org/jtc1/sc22/open/n2356/except.html | If the use of the temporary object can be eliminated without | changing the meaning of the program except for the execution of con- | structors and destructors associated with the use of the temporary | object (_class.temporary_), then the exception in the handler can be | initialized directly with the argument of the throw expression. `---- So, in this program the copy constructor is (I believe) not invoked: #include <iostream> class Foo { public: Foo() {}; Foo(const Foo&) { std::cout<<"copy ctr"<<std::endl; } private: }; int main() { try { throw Foo(); } catch (const Foo &f) { std::cout<<"in the handler"<<std::endl; } return 0; } Best, Bojan -- Bojan Nikolic || http://www.bnikolic.co.uk |