RE: [boost] Re: Standard C Library and C++ / BOOST

Bo Persson wrote:
"Reece Dunn" <msclrhd@hotmail.com> skrev i meddelandet news:BAY7-F123eo6WutfjDM00010dd8@hotmail.com...
Klaus Nowikow wrote:
Reece Dunn wrote: [snip]
A trick I use when handling HRESULT error codes is to have a class like this (adapted for int error type):
class errorcheck { [snip] public: inline errorcheck( int ec ): error( ec ) { if( ec < 0 ) throw( *this ); }
Careful. Throwing an exception from a constructor means that the object won't be constructed (i. e., does not exist). So you are throwing a non-existing object here.
Or am I wrong?
If I've got the standard correct, the object will be partially constructed up to the point where the exception is thrown. Thus, if this throws an exception, the error member will be set.
No, when the constructor fails any partial construction will be undone by calling the destructors for those subobjects.
So the errorcheck object will be in a half-constructed state. That is, the int will be constructed, but the errorcheck object isn't, therefore the int's destructor will be called, but errorcheck's deconstructor will not. The solution would therefore be to throw an intermediate object, like the Trules from the C++ Templates book by David Vandervoorde and Nico Josuttis. This would then get around the throwing in a constructor problem. Regards, Reece _________________________________________________________________ It's fast, it's easy and it's free. Get MSN Messenger today! http://www.msn.co.uk/messenger

From: "Reece Dunn" <msclrhd@hotmail.com>
So the errorcheck object will be in a half-constructed state. That is, the int will be constructed, but the errorcheck object isn't, therefore the int's destructor will be called, but errorcheck's deconstructor will not.
The solution would therefore be to throw an intermediate object, like the Trules from the C++ Templates book by David Vandervoorde and Nico Josuttis. This would then get around the throwing in a constructor problem.
Why not throw a new errorcheck object using a private ctor that stores but doesn't check the return value? -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;
participants (2)
-
Reece Dunn
-
Rob Stewart