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

Re: std::auto_ptr as exception object

by "Alf P. Steinbach" <alfps@[EMAIL PROTECTED] > Apr 30, 2008 at 10:42 AM

* ejstans:
> { 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?

Over in clc++, where you multi-posted this article, James Kanze pointed
out that
the throw expression in the above code is an rvalue; an rvalue can't be
bound to
the formal argument in a T(T&) constructor; for throw a copy constructor
must be
accessible as if that copy constructor was called; and non-const argument
T(T&)
is all that poor std::auto_ptr has in the way of copy constructors.

Hence the code shouldn't even compile.

Unfortunately some current compilers, with all-too-clever implementations
of
std::auto_ptr, do compile the code (although MSVC issues a warning).

Also, over in clc++, where you multi-posted this article, I pointed out
that
it's clearly the intention of the standard that all exception objects
should be
copy constructible, and that you should regard it as an error to throw an
object
that isn't copy constructible (std::auto_ptr isn't copy constructible).

Unfortunately that implicit requirement isn't spelled out anywhere,
AFAICS.


Cheers, & hth.,

- Alf

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

      [ 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 Fri Jul 25 21:57:42 CDT 2008.