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 2 of 5 Topic 9556 of 9823
Post > Topic >>

Re: std::auto_ptr as exception object

by nickf3 <nickf3@[EMAIL PROTECTED] > Apr 28, 2008 at 11:25 PM

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




 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 Thu Jul 24 1:43:22 CDT 2008.