Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: std::auto_p...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 5 Topic 9556 of 9807
Post > Topic >>

Re: std::auto_ptr as exception object

by ejstans <j.l.olsson@[EMAIL PROTECTED] > Apr 29, 2008 at 10:36 AM

On 29 Apr, 07:25, nickf3 <nic...@[EMAIL PROTECTED]
> wrote:
> { Please format your text in 70 columns or so, definitely less than 80.
>    Longer lines are broken at unintended positions and make the text
>    hard to read. -mod }
>
> On Apr 28, 5:59 pm, ejstans <j.l.ols...@[EMAIL PROTECTED]
> wrote:

>
> It's just wrong! auto_ptr was invented for releasing resources when
> leaving scope (including stack unwinding when exception is thrown.)
> The mantra is "throw by value, catch by const reference", the reference
> part being for preserving dynamic type of the exception.
> auto_ptr is a tool for a technique called "Resource Acquisition Is
Initialization",
> or RAII -
seehttp://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization.
> And then there are "exceptional" books from Herb Sutter.
>
> Your original code might even work, but why? What's wrong with throwing
> that int by value? Or with deriving your own context-aware exception
class
> from std::exception?

The original code does in fact not work, because the re-throw uses the
original object, which has lost owner****p. As was explained to me
yesterday,
the standard is not clear but it seems the intention is that
exceptions
must be copy-constructible, which auto_ptr is not. If catching by
reference,
it seems to be okay, but I preferred to be on safer grounds than
merely
assuming it's correct because it seems to work. That's what I meant by
asking if it's "kosher"...
But even if catching auto_ptr by reference is allowed by the standard,
I
prefer not to do so unless it's possible to prevent someone from
catching
by value, which apparently it isn't, so that settles the case...

And I should point out that the code I showed was merely for
illustrative
purposes. Obviously this is not the exact issue I was encountering,
but I
thought it would clearly show the fundamental problem of exceptions
and
auto_ptr.

My exception class is in fact derived from std::exception, but it
happened
to contain an auto_ptr for transferring back owner****p of an object to
the
error-handling code. I am using auto_ptr for its strict owner****p
policy...

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 5 Posts in Topic:
std::auto_ptr as exception object
ejstans <j.l.olsson@[E  2008-04-28 15:59:32 
Re: std::auto_ptr as exception object
nickf3 <nickf3@[EMAIL   2008-04-28 23:25:59 
Re: std::auto_ptr as exception object
ejstans <j.l.olsson@[E  2008-04-29 10:36:59 
Re: std::auto_ptr as exception object
nickf3 <nickf3@[EMAIL   2008-04-29 12:43:22 
Re: std::auto_ptr as exception object
"Alf P. Steinbach&qu  2008-04-30 10:42:04 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 19 19:58:05 CDT 2008.