C++ Logo


Advanced search

Re: [ub] Type punning to avoid copying

From: Jeffrey Yasskin <jyasskin_at_[hidden]>
Date: Mon, 29 Jul 2013 09:30:48 -0700
On Sun, Jul 28, 2013 at 1:50 PM, Richard Smith <richardsmith_at_[hidden]> wrote:
> On Sun, Jul 28, 2013 at 10:42 AM, Nevin Liber <nevin_at_[hidden]>
> wrote:
>> On 28 July 2013 11:44, Gabriel Dos Reis <gdr_at_[hidden]> wrote:
>>> We shouldn't be doing anything in rash.
>> Who is arguing for a rash decision? This sounds like a straw man to me.
>> As far as I can tell, we are exploring how to meet this very real need of
>> programs (efficiently turning a buffer of raw data into something which has
>> structure), which is currently being met (either accidentally or
>> deliberately) by relying on certain undefined behavior not being undefined
>> in any practical sense.
> FWIW, I don't agree that such code necessarily has undefined behavior.
> Consider this ugly code, which is the kind of thing people have been writing
> for years:
> ALIGNED(16) char buffer[BUFFER_SIZE];
> size_t buffer_pos, buffer_read;
> T *get_from_network() {
> if (buffer_pos + sizeof(T) < buffer_read)
> read_more_into_buffer(sizeof(T));
> assert(is_suitably_aligned_for<T>(buffer+buffer_pos));
> return (T*)buffer[buffer_pos];
> }
> Now, [basic.life]p1 says that, unless the object has non-trivial
> initialization, its lifetime begins "when storage with the proper alignment
> and size for type T is obtained". The wording here is circular, because we
> don't know whether an object is being initialized until we know whether its
> lifetime begins, and vice versa, but it can be argued that the lifetime of a
> T object began *before* the data was copied into the buffer, because storage
> with suitable size and alignment was obtained before that point.

I think I like that interpretation as a goal for the wording changes
we need in this area. It gives lots of existing code well-defined
behavior under [basic.types]p3, since the bytes were originally copied
from one T object, stored on disk or a network for a while, and then
copied into a second T object. Would it require any changes to
existing implementations? Are there concrete optimizations we want
that it would ban in the future?


Received on 2013-07-29 18:31:09