C++ Logo

sg12

Advanced search

Re: [ub] type punning through congruent base class?

From: Gabriel Dos Reis <gdr_at_[hidden]>
Date: Thu, 16 Jan 2014 20:00:08 +0000
| On 16 January 2014 21:41, Herb Sutter <hsutter_at_[hidden]> wrote:
| > Matt quoted:
| >
| >
| >
| > "The lifetime of an object of type T begins when:
| >
| > - storage with the proper alignment and size for type T is obtained, and
| >
| > - if the object has non-trivial initialization, its initialization is
| > complete."
| >
| >
| >
| > Perhaps the necessary fix is to strike "if the object has non-trivial
| > initialization" here. I don't see why trivial constructors are special -
| > sure, they don't do anything, but they are notionally important.
| >
| >
| >
| > A bag of memory of alignment and size suitable for T is-not-a T object until
| > its (possibly trivial) ctor is called on that memory - it seems wrong for
| > that to be true for all T except trivially-constructible T's (with implicit
| > universal type-punning for all types <= sizeof(T)).
|
|
| Well, as far as I understand, trivial constructors might not be called at all.

I think you might be referring to the cases where the default constructor does not necessarily initialize a builtin type (or some other C-style types), like in this case:

   #include <string>
    struct A {
         std::string s;
         int i;
    };

   int main() {
         A a; // a.s initialized; but a.i has an indeterminate value
   }

however, I think we do have object a, and subobjects a.s and a.i, with well-defined lifetimes.

-- Gaby

Received on 2014-01-16 21:00:13