[ub] bit_cast
Gabriel Dos Reis
gdr at microsoft.com
Mon Nov 4 19:42:29 CET 2013
| -----Original Message-----
| From: ub-bounces at open-std.org [mailto:ub-bounces at open-std.org] On Behalf Of
| Lawrence Crowl
| Sent: Monday, November 4, 2013 10:40 AM
| To: WG21 UB study group
| Subject: Re: [ub] bit_cast
|
| The intent is to provide an alternative to the idiom
|
| int i; float f = *(float*)&i;
|
| The usual objection is "but memcpy is slow".
| The answer is "your compiler is better than you think".
If we give it enough latitude in terms of non-aliasing :-)
| On 11/4/13, Jeffrey Yasskin <jyasskin at google.com> wrote:
| > template<typename To, typename From>
| > To bit_cast(const From& from) {
| > static_assert(sizeof(To) == sizeof(from));
| > To result;
| > memcpy(result, from, sizeof(from));
| > return result;
| > }
| >
| > On Mon, Nov 4, 2013 at 7:51 AM, J. Daniel Garcia
| > <josedaniel.garcia at uc3m.es> wrote:
| >> Can you elaborate what such a thing would be?
| >>
| >>
| >>
| >> On Mon, Nov 4, 2013 at 5:11 AM, Lawrence Crowl <Lawrence at crowl.org>
| >> wrote:
| >>>
| >>> Did we standardize bit_cast at all?
| >>>
| >>> It would help avoid the almost overwhelming temptation
| >>> for programmers to do the wrong thing.
| >>>
| >>> --
| >>> Lawrence Crowl
| >>> _______________________________________________
| >>> ub mailing list
| >>> ub at isocpp.open-std.org
| >>> http://www.open-std.org/mailman/listinfo/ub
| >>
| >>
| >>
| >> _______________________________________________
| >> ub mailing list
| >> ub at isocpp.open-std.org
| >> http://www.open-std.org/mailman/listinfo/ub
| >>
| > _______________________________________________
| > ub mailing list
| > ub at isocpp.open-std.org
| > http://www.open-std.org/mailman/listinfo/ub
| >
|
|
| --
| Lawrence Crowl
| _______________________________________________
| ub mailing list
| ub at isocpp.open-std.org
| http://www.open-std.org/mailman/listinfo/ub
More information about the ub
mailing list