From -1582107313234593453
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,24c0140d54165625
X-Google-Attributes: gidf78e5,public
From: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Subject: Re: a question about temporary exception object
Date: 1998/11/07
Message-ID: <3643DC0E.EB83C9CD@ix.netcom.com>#1/1
X-Deja-AN: 409372966
Content-Transfer-Encoding: 7bit
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <3642A62C.CF2FC315@ccnet.com>
X-Original-Date: Sat, 07 Nov 1998 00:35:10 -0500
Content-Type: text/plain; charset=us-ascii
Organization: Vast Right-Wing Conspiracy
X-NETCOM-Date: Sat Nov 07  2:36:06 AM CST 1998
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANkRnBuEDnX0m9pzZAQEBAwF/W55iXG3JdZMcscp04vEdjU1GGiKwjVNI gmPsbCni0FJIcUth5NGcn6U4RWl6Ht2w =2uMv
Mime-Version: 1.0
Newsgroups: comp.std.c++

Gilbert Chang wrote:
 > 
 > Consider the following code snippets:
 > 
 > //
 > // class Foo
 > // {
 > // public:
 > //     Foo();
 > //    ~Foo();
 > // };
 > //
 > 
 > try
 > {
 >     throw Foo();
 > }
 > catch(Foo & foo)
 > {
 > }
 > 
 > VS.
 > 
 > try
 > {
 >     throw Foo();
 > }
 > catch(Foo foo)
 > {
 > }
 > 
 > According to the standard, the constructor and the destructor of Foo
 > should be called exactly once for each in the first case, but they
 > could be called either once for each or twice for each.  Is my
 > understanding correct?

In the most literal sense, with no optimizations, the Foo() should cause
a temporary object to be constructed on the stack. Then the throw should
copy the Foo into some safe memory that is unaffected by the stack
unwinding. Then, in the second case, the catch should copy the object a
third time. However, the standard always allow copies of temporaries to
be optimized away, even if their constructors and destructors are
nontrivial, as long as no other semantic changes result. And in
practice, compilers are smart enough to know that the Foo can originally
be constructed into the safe memory where it will live while the stack
is unwound. In fact, the compiler may even construct the foo directly
into the stack space allocated for foo in the second catch clause.
However, I don't think this is typical.

-- 

Ciao,
Paul
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



