[ub] Type punning to avoid copying

Gabriel Dos Reis gdr at cs.tamu.edu
Fri Jul 26 21:26:37 CEST 2013


Ion Gaztañaga <igaztanaga at gmail.com> writes:

| El 26/07/2013 18:10, Gabriel Dos Reis escribió:
| > Jeffrey Yasskin <jyasskin at google.com> writes:
| >
| > | On Fri, Jul 26, 2013 at 8:30 AM, Gabriel Dos Reis <gdr at cs.tamu.edu> wrote:
| > | > Ion Gaztañaga <igaztanaga at gmail.com> writes:
| > | >
| > | > [...]
| > | >
| > | > | ¿How can we tell the compiler that a memory buffer is really a different
| > | > | type?
| > | >
| > | > Invoke a constructor to turn the raw memory into an object of the
| > | > desired type.
| > |
| > | Can you point to the wording that explains the behavior in that case?
| >
| > I am not sure I understand your request.  Ion wants to state a region of
| > storage is of a given type.  The behavior after the contructor ran is
| > what you get after running a constructor -- see section 12.1.
| 
| We don't want to run the constructor because the constructor might touch 
| bytes that are already set by an external source, we want to tell the 
| compiler that the object is already correctly constructed.

If you want a C++ object, you want it to have a lifetime with
well-defined invariants.  There is no notion of an object's value before
it is constructed or before its lifetime started.

-- Gaby


More information about the ub mailing list