Boost logo

Boost :

From: David Bergman (davidb_at_[hidden])
Date: 2002-08-14 13:43:36


Good point, Peter.

To accentuate that there is no slicing, one can actually add an instance
variable, as in:

        struct Y: public X { Y() { value=42; }; int value; };

and output that value in the "catch" in main. There is no slicing.

It was a long time since I wrote my first C++ compiler front-end (back
when neither templates nor exceptions were supported by any compiler),
but the compiler has to insert code to allocate that thrown exception
(in this case of size 4 on most platforms) in "a special place", or? The
copy constructor (whose address also needs to be saved, linked or
unlinked, in an exception table data structure in the compiled file) is
then called upon catching the exception.

Thus, there is no copying while unwinding the stack, since the original
exception is kept "in a very special place", and the address of the copy
constructor is saved in an exception table area. That address is used at
the catch-point. After the copy constructor is called, the "special
place" can be deallocated.

There have been a lot of arguments about slicing...

        1. Nothing is sliced, prior to invoking the copy constructor
        2. But, we obviously need to have the address of the correct
copy constructor available (in this case Y...). This is where the
"slicing" can occur, if we only have a copy constructor of a base class
available, as in "std_exception"...

If I am mistaken, please inform me, since my 9 years outside the
compiler arena might have made me a bit rusty ;-)

/David

-----Original Message-----
From: boost-bounces_at_[hidden]
[mailto:boost-bounces_at_[hidden]] On Behalf Of Peter Dimov
Sent: Wednesday, August 14, 2002 1:54 PM
To: boost_at_[hidden]
Subject: Re: [boost] std::exception -- Re: Re: Re:
Re:Re:AttemptingresolutionofThreads&ExceptionsIssue

From: "Victor A. Wagner, Jr." <vawjr_at_[hidden]>
> >
> >Well, I am not a compiler writer, but it seems to me that to
implement
> >"throw;" and "catch", the compiler already needs a way to copy the
> >exception, complete with its original type. :-)
>
> I don't see that at all. The catch() clause specifies the type it's
> expecting and it will be copied and sliced if need be to match.

Quiz: what does this program do?

#include <iostream>

struct X
{
};

struct Y: public X
{
};

void f()
{
    try
    {
        throw Y();
    }
    catch(X)
    {
        throw;
    }
}

int main()
{
    try
    {
        f();
    }
    catch(Y)
    {
        std::cout << "Caught an Y!\n";
    }
}

_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk