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 21:04:43 -0800
Kazutoshi Satoda <k_satoda_at_[hidden]> writes:

| On 2014/01/17 12:57 +0900, Kazutoshi Satoda wrote:
| > (Note)
| > In C, there is no such a thing like "an object of type int" unlike in
| > C++ where an object has a type and the type is determined when the
| > object is created. In C, "object" is a merely a region of data storage,
| > and may be labeled by an effective type which is determined by ongoing
| > or previous access to the object at a time.
| >
| > If "struct B x = *p" follows the above code, the access (lvalue
| > conversion, 6.3.2.1 p2) on *p, which is allowed by the aliasing rule
| > (6.5 p7, bullet 5 "an aggregate or union ..."), changes the effective
| > type of the object at p.
|
| Uh, sorry. I was wrong here. Non-modifying access to an object doesn't
| change the effective type of subsequent access to the object. And the
| aliasing rule does not apply here.

Aha, but what about 6.5/6

   [...] For all other accesses to an object having no declared type,
   the effective type of the object is simply the type of the lvalue use
   for the access.

?

| Let me correct the last paragraph.
|
| If "struct B x = {0}; *p = x;" follows the above code, the write on *p
| changes the effective type of the object at p (from int to B) for
| subsequent non-modifying access of that object. (6.5 p6, about an
| object having no declared type)

C++ has a simpler notation for that:

      new (p) B{ 0 };

but we have been there before :-)

-- Gaby

Received on 2014-01-17 06:04:46