C++ Logo

sg12

Advanced search

Re: [ub] Type punning to avoid copying

From: Gabriel Dos Reis <gdr_at_[hidden]>
Date: Fri, 26 Jul 2013 14:22:07 -0500
Jeffrey Yasskin <jyasskin_at_[hidden]> writes:

[...]

| Ah, that's too bad because it doesn't serve the purpose Ion was asking
| about. He wanted a memory buffer with known contents to become the
| object representation of an object of a particular type. If the object
| post-construction has an indeterminate object representation instead,
| he can't use a constructor to turn the raw memory into an object with
| the desired value.

The oblique point I was making in the original reply was that C++'s
model requires that we work with objects, which means we work with
constructors and invariants guaranteed during construction or during the
object's lifetime. Maybe that was too oblique :-(

We don't have a notion of an object's value before it existed.

| Howard, if Gaby's right that new(&memory) int; leaves the object with
| indeterminate value even if 'memory' started with a defined value (and
| I think he is), then that's the rule you're breaking when you
| explicitly change the active member of your union and then read the
| new member.

I think so. It looks to me as if some of the "issues" raised in this
discussion implicitly make the assumption that there is a notion of an
object's value before its construction or lifetime, and such value is
preserved after construction. If that is the case, then I think those
issues are not properly formulated for C++'s fundamental object model.

-- Gaby

Received on 2013-07-26 21:22:24