Topic: POD-struct + destructor != POD-struct, why?


Author: chrishdp@uclink.berkeley.edu (Christopher Jon Nokleberg)
Date: 1998/07/02
Raw View
Draft C++ standard, section 9.4:
  A POD   struct is an aggregate class that has no non   static data
  members of type pointer to member, non   POD   struct, non   POD   union
  (or array of such types) or reference, and has no user   defined
  copy assignment operator and no user   defined destructor.

Could someone explain to me the technical reasons why a class with a
user-defined destructor cannot be a POD-struct? I was relying on the
following behavior for a particular class, but with a destructor it
only works with some compilers:

  A pointer to a POD   struct object, suitably converted, points to
  its initial member (or if that member is a bit   field, then to the
  unit in which it resides) and vice versa.

If some compilers can do it, why not all?

Thanks,
Chris
---
[ 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              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/07/02
Raw View
Christopher Jon Nokleberg wrote:
>
> Draft C++ standard, section 9.4:
>   A POD-struct is an aggregate class that has no non-static data
>   members of type pointer to member, non-POD-struct, non-POD-union
>   (or array of such types) or reference, and has no user-defined
>   copy assignment operator and no user-defined destructor.
>
> Could someone explain to me the technical reasons why a class with a
> user-defined destructor cannot be a POD-struct? I was relying on the
> following behavior for a particular class, but with a destructor it
> only works with some compilers:
>
>   A pointer to a POD-struct object, suitably converted, points to
>   its initial member (or if that member is a bit-field, then to the
>   unit in which it resides) and vice versa.
>
> If some compilers can do it, why not all?

The concept of a POD was invented to describe C-style structs, which use
none of the class-specific features that C++ added to the concept of a
struct. The behavior you cite is required only for POD-structs. However,
I know of no reason why it would fail for a class that failed to qualify
as a POD-struct only because it had a non-virtual destructor. Did you
define a virtual destructor for this class? If so, then the compilers
where it didn't work probably inserted a vtbl pointer as the hidden
first member of your class.


[ 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              ]