Can catching an exception by const ref (instead of just plain ref) avoid creating a temporary?

Can catching an exception by const ref (instead of just plain ref) avoid creating a temporary?

Post by Lorin Hochstei » Wed, 19 Jul 2000 04:00:00



In a recent discussion in comp.objects.corba

remarked that catching an exception by reference to const, e.g.

catch(const CORBA::SystemException &ex)
{
...

Quote:}

can sometimes avoid construction of a temporary that is created when you
just catch a reference, e.g.

catch(CORBA::SystemException &ex)
{
...

Quote:}

Unfortunately, Michi couldn't remember where he had read this, and so
couldn't provide an example. Since this is a C++ issue, I figured I'd
consult the C++ gurus. Does anybody know if catching an exception by
reference to const (as opposed to just catching by reference) can save you
the construction of a temporary? Could someone give an example?

Yours,

Lorin Hochstein

(P.S. I know that if you DON'T catch by reference, you'll create a
temporary, and slice off the derived part of the object if you catch it by a
base class. That isn't the question...)

 
 
 

Can catching an exception by const ref (instead of just plain ref) avoid creating a temporary?

Post by Mark Wilde » Wed, 19 Jul 2000 04:00:00



Quote:> Does anybody know if catching an exception by
> reference to const (as opposed to just catching by reference) can save you
> the construction of a temporary?

The first reaction I have is to ask whether your exception objects are very
expensive to create, or whether you're throwing lots and lots of them...

 
 
 

Can catching an exception by const ref (instead of just plain ref) avoid creating a temporary?

Post by Neil Butterwort » Thu, 20 Jul 2000 04:00:00



> In a recent discussion in comp.objects.corba

Henning
> remarked that catching an exception by reference to const, e.g.

> catch(const CORBA::SystemException &ex)
> {
> ...
> }

> can sometimes avoid construction of a temporary that is created when you
> just catch a reference, e.g.

> catch(CORBA::SystemException &ex)
> {
> ...
> }

> Unfortunately, Michi couldn't remember where he had read this, and so
> couldn't provide an example. Since this is a C++ issue, I figured I'd
> consult the C++ gurus. Does anybody know if catching an exception by
> reference to const (as opposed to just catching by reference) can save you
> the construction of a temporary? Could someone give an example?

const'ness should have no effect. Assuming you throw an exception object,
the catch() will get a reference to it (no temporary involved). The const
simply says what you're allowed to do to the exception object for which you
have a reference.

NeilB

 
 
 

Can catching an exception by const ref (instead of just plain ref) avoid creating a temporary?

Post by John Celme » Thu, 20 Jul 2000 04:00:00


I think a temporary is always created when an exception is thrown.  Right?




> > In a recent discussion in comp.objects.corba

> Henning
> > remarked that catching an exception by reference to const, e.g.

> > catch(const CORBA::SystemException &ex)
> > {
> > ...
> > }

> > can sometimes avoid construction of a temporary that is created when you
> > just catch a reference, e.g.

> > catch(CORBA::SystemException &ex)
> > {
> > ...
> > }

> > Unfortunately, Michi couldn't remember where he had read this, and so
> > couldn't provide an example. Since this is a C++ issue, I figured I'd
> > consult the C++ gurus. Does anybody know if catching an exception by
> > reference to const (as opposed to just catching by reference) can save
you
> > the construction of a temporary? Could someone give an example?

> const'ness should have no effect. Assuming you throw an exception object,
> the catch() will get a reference to it (no temporary involved). The const
> simply says what you're allowed to do to the exception object for which
you
> have a reference.

> NeilB

 
 
 

Can catching an exception by const ref (instead of just plain ref) avoid creating a temporary?

Post by Lorin Hochstei » Thu, 20 Jul 2000 04:00:00





> > Does anybody know if catching an exception by
> > reference to const (as opposed to just catching by reference) can save
you
> > the construction of a temporary?

> The first reaction I have is to ask whether your exception objects are
very
> expensive to create, or whether you're throwing lots and lots of them...

It's really more of an academic question. I'm not concerned so much about
actual performance (since exceptions slow things down anyways, if they get
thrown) or memory usage.
 
 
 

Can catching an exception by const ref (instead of just plain ref) avoid creating a temporary?

Post by richardl.. » Thu, 20 Jul 2000 04:00:00


Yes, using a const reference has many advantages.
Firstly, as it is a refrence, you can access the virtual functions of
exceptions derived from CORBA::SystemException.
As to the question of temporary objects, I did once come accross this
in some code I was writing.
A reference will allow you to change the original object. To prevent
this, a C++ implementation can create a copy of the original and give
you a reference to that. But if you use a const reference, there is no
need for that, as you can't change the original.



> In a recent discussion in comp.objects.corba

Henning
> remarked that catching an exception by reference to const, e.g.

> catch(const CORBA::SystemException &ex)
> {
> ...
> }

> can sometimes avoid construction of a temporary that is created when
you
> just catch a reference, e.g.

> catch(CORBA::SystemException &ex)
> {
> ...
> }

> Unfortunately, Michi couldn't remember where he had read this, and so
> couldn't provide an example. Since this is a C++ issue, I figured I'd
> consult the C++ gurus. Does anybody know if catching an exception by
> reference to const (as opposed to just catching by reference) can
save you
> the construction of a temporary? Could someone give an example?

> Yours,

> Lorin Hochstein

> (P.S. I know that if you DON'T catch by reference, you'll create a
> temporary, and slice off the derived part of the object if you catch
it by a
> base class. That isn't the question...)

Sent via Deja.com http://www.deja.com/
Before you buy.
 
 
 

Can catching an exception by const ref (instead of just plain ref) avoid creating a temporary?

Post by Neil Butterwort » Sat, 22 Jul 2000 04:00:00



Quote:> I think a temporary is always created when an exception is thrown.  Right?

If you do:

class C { ... };
...
throw C;

then, "Yes".

But to nitpick a bit, if I do:

throw new C;            // kids, just say "No" to this!

then a "temporary" is not created.

And  if in the original "throw C"  scenario you do:

try {
    SomethingThatMightThrowC();

Quote:}

catch ( C ce ) {        // catch by value

Quote:}

A named object "ce" (not a temporary)  is created (via the copy constructor)
in the catch block. This copying is undesirable, which is why
catch-by-reference is the best way to go.

NeilB

 
 
 

Can catching an exception by const ref (instead of just plain ref) avoid creating a temporary?

Post by John Celme » Sat, 22 Jul 2000 04:00:00



Quote:> try {
>     SomethingThatMightThrowC();
> }
> catch ( C ce ) {        // catch by value
> }

> A named object "ce" (not a temporary)  is created (via the copy
constructor)
> in the catch block. This copying is undesirable, which is why
> catch-by-reference is the best way to go.

I'll agree that you picked the right nit.
However, with regards to your last remark, that catch-by-reference is the
best
way to go.  That may not necessarily be true.  You could:

try {
     SomethingThatMightThrowC();

Quote:}

catch ( C ) {        // catch a temporary
Quote:}