C++ Logo

sg12

Advanced search

Re: [ub] Type punning to avoid copying

From: Ion Gaztaņaga <igaztanaga_at_[hidden]>
Date: Fri, 26 Jul 2013 00:06:44 +0200
El 25/07/2013 17:45, Gabriel Dos Reis escribiķ:
> If that analysis is correct, I would welcome a rule in the language that
> says that memcpy() into an object of type that admits no trap
> representation is equivalent to initialization with value obtained from
> appropriate interpretation of the value representation bits of the
> "initializing byte" sequence.

Why can't we memcpy to a type that admits a trap representation (say, a
float with a signaling NaN value)? If we know about that representation
is in our system, the programmer should be responsible for not creating
such bit pattern before copying it to the destination. Couldn't we write:

float f_uninitialized;
std::memcpy(&f_uninitialized, &i32, sizeof(i32));

?

Best,

Ion

Received on 2013-07-26 00:06:52