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! ]


|