{ 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:
> { Multi-posted article. -mod }
>
> Hello,
>
> I am using std::auto_ptr to try to keep owner****p of resources
> straight. However, I became aware that exception objects must provide
> a copy constructor, which made me uncertain of the soundness of
> throwing std::auto_ptrs...
>
> It was explained to me that a problem is:
>
> try {
> throw std::auto_ptr<int>(new int(10));
>
> } catch (std::auto_ptr<int> a) {
> throw;
> }
>
> If I understood correctly, the re-throw would use the original
> exception object, which has lost owner****p.
> Is there a way to prevent catching by value?
> Is catching by reference kosher?
> Are there other problems with throwing auto_ptrs?
>
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 - see
http://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?
--
Nikolai
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|