www.digitalmars.com         C & C++   DMDScript  

c++ - Catch slicing

reply "Matthew Wilson" <matthew stlsoft.org> writes:
Walter

How hard would it be to add a (non-default, I suppose) flag (-wxs) to warn
when catching by value for non-fundamental, or for non-POD, types, as in:

class A
{};

class B
  : public A
{};

try
{
  throw B()
}
catch(A a) // "Warning: Catch clause type is by value for polymorphic type"
{

}

It'd be sweet (and would get DMC++ a special mention in Part 5, which I may
do next).

Matthew
Aug 17 2003
parent reply "Walter" <walter digitalmars.com> writes:
It's not so easy to tell that a class has been derived from, such as if
class B is in another file.

"Matthew Wilson" <matthew stlsoft.org> wrote in message
news:bhpg7o$1sfb$1 digitaldaemon.com...
 Walter

 How hard would it be to add a (non-default, I suppose) flag (-wxs) to warn
 when catching by value for non-fundamental, or for non-POD, types, as in:

 class A
 {};

 class B
   : public A
 {};

 try
 {
   throw B()
 }
 catch(A a) // "Warning: Catch clause type is by value for polymorphic
type"
 {

 }

 It'd be sweet (and would get DMC++ a special mention in Part 5, which I
may
 do next).

 Matthew
Aug 20 2003
parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
I appreciate that, but surely it's possible to readily deduce that a non-POD
type is being caught by value?

"Walter" <walter digitalmars.com> wrote in message
news:bi1l46$1lbs$4 digitaldaemon.com...
 It's not so easy to tell that a class has been derived from, such as if
 class B is in another file.

 "Matthew Wilson" <matthew stlsoft.org> wrote in message
 news:bhpg7o$1sfb$1 digitaldaemon.com...
 Walter

 How hard would it be to add a (non-default, I suppose) flag (-wxs) to
warn
 when catching by value for non-fundamental, or for non-POD, types, as
in:
 class A
 {};

 class B
   : public A
 {};

 try
 {
   throw B()
 }
 catch(A a) // "Warning: Catch clause type is by value for polymorphic
type"
 {

 }

 It'd be sweet (and would get DMC++ a special mention in Part 5, which I
may
 do next).

 Matthew
Aug 20 2003
parent reply "Walter" <walter digitalmars.com> writes:
What's a POD?

"Matthew Wilson" <matthew stlsoft.org> wrote in message
news:bi1piu$1rrj$1 digitaldaemon.com...
 I appreciate that, but surely it's possible to readily deduce that a
non-POD
 type is being caught by value?

 "Walter" <walter digitalmars.com> wrote in message
 news:bi1l46$1lbs$4 digitaldaemon.com...
 It's not so easy to tell that a class has been derived from, such as if
 class B is in another file.

 "Matthew Wilson" <matthew stlsoft.org> wrote in message
 news:bhpg7o$1sfb$1 digitaldaemon.com...
 Walter

 How hard would it be to add a (non-default, I suppose) flag (-wxs) to
warn
 when catching by value for non-fundamental, or for non-POD, types, as
in:
 class A
 {};

 class B
   : public A
 {};

 try
 {
   throw B()
 }
 catch(A a) // "Warning: Catch clause type is by value for polymorphic
type"
 {

 }

 It'd be sweet (and would get DMC++ a special mention in Part 5, which
I
 may
 do next).

 Matthew
Aug 21 2003
parent reply "Matthew Wilson" <dmd synesis.com.au> writes:
Plain Old Data. Stuff with ctors/dtors/copy-assignment ops/etc.

I'm pretty sure it's defined in the standard. I'll have a look.

"Walter" <walter digitalmars.com> wrote in message
news:bi4cq6$2n5h$1 digitaldaemon.com...
 What's a POD?

 "Matthew Wilson" <matthew stlsoft.org> wrote in message
 news:bi1piu$1rrj$1 digitaldaemon.com...
 I appreciate that, but surely it's possible to readily deduce that a
non-POD
 type is being caught by value?

 "Walter" <walter digitalmars.com> wrote in message
 news:bi1l46$1lbs$4 digitaldaemon.com...
 It's not so easy to tell that a class has been derived from, such as
if
 class B is in another file.

 "Matthew Wilson" <matthew stlsoft.org> wrote in message
 news:bhpg7o$1sfb$1 digitaldaemon.com...
 Walter

 How hard would it be to add a (non-default, I suppose) flag (-wxs)
to
 warn
 when catching by value for non-fundamental, or for non-POD, types,
as
 in:
 class A
 {};

 class B
   : public A
 {};

 try
 {
   throw B()
 }
 catch(A a) // "Warning: Catch clause type is by value for
polymorphic
 type"
 {

 }

 It'd be sweet (and would get DMC++ a special mention in Part 5,
which
 I
 may
 do next).

 Matthew
Aug 21 2003
next sibling parent "Matthew Wilson" <dmd synesis.com.au> writes:
3.9

"Matthew Wilson" <dmd synesis.com.au> wrote in message
news:bi4cta$2n97$1 digitaldaemon.com...
 Plain Old Data. Stuff with ctors/dtors/copy-assignment ops/etc.

 I'm pretty sure it's defined in the standard. I'll have a look.

 "Walter" <walter digitalmars.com> wrote in message
 news:bi4cq6$2n5h$1 digitaldaemon.com...
 What's a POD?

 "Matthew Wilson" <matthew stlsoft.org> wrote in message
 news:bi1piu$1rrj$1 digitaldaemon.com...
 I appreciate that, but surely it's possible to readily deduce that a
non-POD
 type is being caught by value?

 "Walter" <walter digitalmars.com> wrote in message
 news:bi1l46$1lbs$4 digitaldaemon.com...
 It's not so easy to tell that a class has been derived from, such as
if
 class B is in another file.

 "Matthew Wilson" <matthew stlsoft.org> wrote in message
 news:bhpg7o$1sfb$1 digitaldaemon.com...
 Walter

 How hard would it be to add a (non-default, I suppose) flag (-wxs)
to
 warn
 when catching by value for non-fundamental, or for non-POD, types,
as
 in:
 class A
 {};

 class B
   : public A
 {};

 try
 {
   throw B()
 }
 catch(A a) // "Warning: Catch clause type is by value for
polymorphic
 type"
 {

 }

 It'd be sweet (and would get DMC++ a special mention in Part 5,
which
 I
 may
 do next).

 Matthew
Aug 21 2003
prev sibling parent reply Heinz Saathoff <hsaat bre.ipnet.de> writes:
Matthew Wilson schrieb...
 
 Plain Old Data. Stuff with ctors/dtors/copy-assignment ops/etc.
^^^^ without POD structs should only consist of simple data members. No member functions at all. POD type variables can be safely copied with memcopy. - Heinz
Aug 22 2003
next sibling parent "Matthew Wilson" <dmd synesis.com.au> writes:
Yes, it was a mistake.

And yes, I agree with all that you've said about POD

"Heinz Saathoff" <hsaat bre.ipnet.de> wrote in message
news:MPG.19afdfd9e86c795f9896ce news.digitalmars.com...
 Matthew Wilson schrieb...
 Plain Old Data. Stuff with ctors/dtors/copy-assignment ops/etc.
^^^^ without POD structs should only consist of simple data members. No member functions at all. POD type variables can be safely copied with memcopy. - Heinz
Aug 22 2003
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
Hmm. I would think that a POD type would be defined by not having a vptr.

"Heinz Saathoff" <hsaat bre.ipnet.de> wrote in message
news:MPG.19afdfd9e86c795f9896ce news.digitalmars.com...
 Matthew Wilson schrieb...
 Plain Old Data. Stuff with ctors/dtors/copy-assignment ops/etc.
^^^^ without POD structs should only consist of simple data members. No member functions at all. POD type variables can be safely copied with memcopy. - Heinz
Aug 25 2003
parent reply Heinz Saathoff <hsaat bre.ipnet.de> writes:
Walter schrieb...
 
 Hmm. I would think that a POD type would be defined by not having a vptr.
Not only that struct but also included structs. The standard says that PODs are classic C-structs which makes sense. This also gives compiler writes more freedom in implementing things. For DMC I know that adding members isn't a problem and I still can memcpy such structs. But it's not guaranteed by the standard. - Heinz
Aug 25 2003
parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
Maybe we could have two levels

 -wxs1 - whatever meaningful definition of non-POD types being caught by
value (not reference or ptr)
 -wxs2 - any type other than fundamental types being caught by value (not
reference or ptr)

For myself I'd use -wxs2 all the time



"Heinz Saathoff" <hsaat bre.ipnet.de> wrote in message
news:MPG.19b3f7e93fbe82389896cf news.digitalmars.com...
 Walter schrieb...
 Hmm. I would think that a POD type would be defined by not having a
vptr.
 Not only that struct but also included structs. The standard says that
 PODs are classic C-structs which makes sense. This also gives compiler
 writes more freedom in implementing things. For DMC I know that adding
 members isn't a problem and I still can memcpy such structs. But it's
 not guaranteed by the standard.

 - Heinz
Aug 25 2003
parent reply "Philippe Mori" <philippe_mori hotmail.com> writes:
"Matthew Wilson" <matthew stlsoft.org> a écrit dans le message de
news:bicnia$938$1 digitaldaemon.com...
 Maybe we could have two levels

  -wxs1 - whatever meaningful definition of non-POD types being caught by
 value (not reference or ptr)
  -wxs2 - any type other than fundamental types being caught by value (not
 reference or ptr)

 For myself I'd use -wxs2 all the time
IMO, the second option is enough... Even for POD type, you should catch them by reference as it is possible to derive from a structure and will then have the slicing problem again if you throw again the exception. I don't see much benefit in catch by value.... but then what is the difference with passing parameters to a function by value!
Aug 27 2003
parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
"Philippe Mori" <philippe_mori hotmail.com> wrote in message
news:biirgo$ida$1 digitaldaemon.com...
 "Matthew Wilson" <matthew stlsoft.org> a écrit dans le message de
 news:bicnia$938$1 digitaldaemon.com...
 Maybe we could have two levels

  -wxs1 - whatever meaningful definition of non-POD types being caught by
 value (not reference or ptr)
  -wxs2 - any type other than fundamental types being caught by value
(not
 reference or ptr)

 For myself I'd use -wxs2 all the time
IMO, the second option is enough... Even for POD type, you should catch them by reference as it is possible to derive from a structure and will
then
 have the slicing problem again if you throw again the exception. I don't
see
 much benefit in catch by value.... but then what is the difference with
 passing
 parameters to a function by value!
Well, one thing with pbv is that it is a must when used on iterators in algorithms. Overall, I agree that only the second would be of use to me, but I was just wanting to cater for sloppy programmers and hard-pressed compiler implementers
Aug 27 2003
parent "Philippe Mori" <philippe_mori hotmail.com> writes:
 Well, one thing with pbv is that it is a must when used on iterators in
 algorithms.
It is for catch clauses that pass-by-value has no reel value for me... For other cases like function parameters, it is different.
Aug 29 2003