Thread: Is this safe?

  1. #16
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Sorry to bother you again, but after trying to read the standard (for the first time) and discussing with some mates one point is still not clear:

    Quote Originally Posted by CornedBee View Post
    Because Derived has a base class, it's not a POD, and therefore accessing its bytes via the cast to char* is undefined.
    So,

    example 1

    Code:
    class A
    {
      int a;
    }
    
    class B
    {
      A a;
      int b;
    };
    Class A is a POD and class B is a POD, right?


    example 2:

    Code:
    class A
    {
      int a;
    }
    
    class B :public A
    {
      int b;
    };
    Class A is a POD and class B is none due to having a base class member?

    Is this the sentence of the standard, which is saying that?


    3.9 Types [basic.types]

    1 [ Note: 3.9 and the subclauses thereof impose requirements on implementations regarding the representation of types.
    There are two kinds of types: fundamental types and compound types. Types describe objects (1.8), references (8.3.2),
    or functions (8.3.5). —end note ]

    2 For any object (other than a base-class subobject) of POD type T, whether or not the object holds a valid value of type T,
    the underlying bytes (1.7) making up the object can be copied into an array of char or unsigned char.41) If the content
    of the array of char or unsigned char is copied back into the object, the object shall subsequently hold its original
    value.

    Example:
    # define N sizeof (T)
    char buf[N];
    T obj ; / / obj initialized to its original value
    std :: memcpy (buf , & obj , N); / / between these two calls to std::memcpy,
    / / obj might be modified
    std :: memcpy (& obj , buf , N); / / at this point, each subobject of obj of scalar type
    / / holds its original value
    —end example ]
    Or could you be so kind to point me to the section of the standard which declares, what classes which have nonvirtual base classes arn't PODs?

    Or did you talk about virtual inheritance only?

    Thank you!

  2. #17
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    I also found at http://en.wikipedia.org/wiki/C%2B%2B0x
    this description of POD for C++0x (and it mentioned, what C++0x will be more relaxed in that case than 03)

    C++0x will relax several rules with regard to the POD definition.

    A class/struct is considered a POD if it is trivial, standard-layout, and it has no non-static members that are not PODs. A trivial class or struct is defined as one that:

    1. Has a trivial default constructor. This may use the default constructor syntax (SomeConstructor() = default.
    2. Has a trivial copy constructor, which may use the default syntax.
    3. Has a trivial copy assignment operator, which may use the default syntax.
    4. Has a trivial destructor, which may not be virtual.

    A standard-layout class or struct is defined as one that:

    1. Has only non-static data members that are of standard-layout type
    2. Has the same access control (public, private, protected) for all non-static members
    3. Has no virtual functions
    4. Has no virtual base classes
    5. Has only base classes that are of standard-layout type
    6. Has no bases classes of the same type as the first defined non-static member
    7. Either has no base classes with non-static members, or has no non-static data members in the most derived class and at most one base class with non-static members. In essence, there may be only one class in this classes hierarchy that has non-static members.
    So if I understand Point 7, than it's really that strict regarding classes with base classes: Almost none of them are PODs, right?

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Haven't looked very much at the C++0x POD relaxation, so I won't comment on it.

    In your 2 examples, your analysis is right, except that you need to make all members public - any private data members destroy POD-ness.

    As for the standard, the section you quoted talks about what you can do with PODs, not what PODs are.
    Standard references:
    POD struct:
    Quote Originally Posted by 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.
    aggregate class:
    Quote Originally Posted by 8.5.1/1
    An aggregate is an array or a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions.
    These together make up the requirements on POD structs (don't let the name mislead - the class keyword can also start a POD struct, although it rarely does).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Thanks again. I think I got it now

  5. #20
    Registered User
    Join Date
    May 2006
    Posts
    630
    When to use reinterpret_cast in c++? Is there any need for it?

    I only use static_cast and dynamic_cast..

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    When static_cast fails, you need to rethink everything and then use reinterpret_cast.

    In particular, you'll find that many of the casts that are safe only for PODs only work with reinterpret_cast.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #22
    Registered User
    Join Date
    May 2006
    Posts
    630
    What means 'being safe only for PODs' ?

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Read the thread.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  2. Bjarne's exception safe sample
    By George2 in forum C++ Programming
    Replies: 13
    Last Post: 12-28-2007, 05:38 PM
  3. A Safe Dialect of C
    By viaxd in forum Tech Board
    Replies: 11
    Last Post: 11-26-2003, 11:14 AM
  4. How safe is it?
    By hermit in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 05-08-2002, 09:33 PM
  5. Safe Mode on FreeBsd
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-25-2001, 09:37 AM