Boost logo

Boost :

Subject: Re: [boost] [config] RFC PR 82
From: Agustín K-ballo Bergé (kaballo86_at_[hidden])
Date: 2015-11-24 17:01:14


On 11/24/2015 6:44 PM, Andrey Semashev wrote:
> On 2015-11-25 00:21, Agustín K-ballo Bergé wrote:
>> On 11/24/2015 6:03 PM, Andrey Semashev wrote:
>>> On 2015-11-24 23:54, Agustín K-ballo Bergé wrote:
>>>> On 11/24/2015 5:20 PM, Andrey Semashev wrote:
>>>>>>> Ditto BOOST_HAS_UNION_TYPE_PUNNING_TRICK (doesn't any compiler
>>>>>>> support
>>>>>>> this?).
>>>>>>
>>>>>> 'I'm all with you on this one' but since 'it is not in the standard'
>>>>>> language purists will probably complain if it is used
>>>>>> unconditionally...
>>>>>
>>>>> To some extent this is guaranteed by [class.union]/1 in C++11.
>>>>
>>>> No, it isn't.
>>>
>>> Why? Reading different members of the standard layout union within the
>>> common initial sequence is enough to implement a bitwise_cast.
>>
>> That would be 9.2 [class.mem]/19, notes are not normative.
>>
>> I can't tell from the context whether this would be enough for a
>> `bitwise_cast` (what is it supposed to do?). The OP talks about "union
>> type punning trick", which is a fine practice in C11 but undefined
>> behavior in C++. Some compilers choose to offer this as a conforming
>> extension.
>
> I'm not sure I understand. This definitely was UB in C++03, but that
> addition you pointed to makes it defined behavior in C++11.

That bit I pointed to has always been there as far as I know, and I can
confirm it is there in C++03 (9.2 [class.mem]/16).

> By bitwise_cast I meant something along these lines:
>
> template< typename To, typename From >
> To bitwise_cast(From from)
> {
> union
> {
> From as_from;
> To as_to;
> }
> caster = { from };
> return caster.as_to;
> }
>
> With certain restrictions on From and To types I think this code has a
> well defined behavior in C++11.

Those restrictions would be that `To` and `From` are layout-compatible,
and that both `To` and `From` are standard-layout struct types.

Regards,

-- 
Agustín K-ballo Bergé.-
http://talesofcpp.fusionfenix.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk