Boost logo

Boost :

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


Peter,

One "subtle" note, though. You are aware that what you threw in 'f' was
actually the original object? The copy-constructor of X *does* indeed
slice (they always do, them copy-constructors ;-). If you want to see
the effect of this X object being passed up, and caught as an Y, change
the code to:

#include <iostream>

struct X
{
};

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

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

int main()
{
    try
    {
        f();
    }
    catch(Y y)
    {
      std::cout << "Caught an Y, with value " << y.value << std::endl;
    }
}

This will give you some sliced problems.

Remember, copy-constructors, implicit or explicit, *do* slice.

/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