1. Background
Low-level code often seeks to interpret objects of one type as another: keep the
same bits, but obtain an object of a different type. Doing so correctly is
error-prone: using
or
runs afoul of type-aliasing
rules yet these are the intuitive solutions developers mistakenly turn to.
Attuned developers use
with
, avoiding alignment
pitfalls and allowing them to bit-cast non-default-constructible types.
This facility inevitably ends up being used incorrectly on pointer types, we
propose using appropriate concepts to prevent misuse. As our sample
implementation demonstrates we could as well use
or template
SFINAE, but the timing of this library feature will likely coincide with
concept’s standardization.
Furthermore, it is currently impossible to implement a
bit-cast
function, as
itself isn’t
. Marking our proposed function as
doesn’t require or prevent
from becoming
. This
leaves implementations free to use their own internal solution (e.g. LLVM has a
opcode).
We propose to standardize this oft-used idiom, and avoid the pitfalls once and for all.
2. Proposed Wording
Below, substitute the �
character with a number the editor finds appropriate
for the sub-section.
2.1. Synopsis
Under 20.2 Header
synopsis [utility]:
namespace std { // ... // 20.2.� bit-casting: template < typename To , typename From > requires sizeof ( To ) == sizeof ( From ) && is_trivially_copyable_v < To > && is_trivially_copyable_v < From > && is_standard_layout_v < To > && is_standard_layout_v < From > && ! ( is_pointer_v < From > && is_pointer_v < To > ) && ! ( is_member_pointer_v < From > && is_member_pointer_v < To > ) && ! ( is_member_object_pointer_v < From > && is_member_object_pointer_v < To > ) && ! ( is_member_function_pointer_v < From > && is_member_function_pointer_v < To > ) constexpr To bit_cast ( const From & from ) noexcept ; // ... }
2.2. Details
Under 20.2.�
Bit-casting [utility.bitcast]:
template < typename To , typename From > requires sizeof ( To ) == sizeof ( From ) && is_trivially_copyable_v < To > && is_trivially_copyable_v < From > && is_standard_layout_v < To > && is_standard_layout_v < From > && ! ( is_pointer_v < From > && is_pointer_v < To > ) && ! ( is_member_pointer_v < From > && is_member_pointer_v < To > ) && ! ( is_member_object_pointer_v < From > && is_member_object_pointer_v < To > ) && ! ( is_member_function_pointer_v < From > && is_member_function_pointer_v < To > ) constexpr To bit_cast ( const From & from ) noexcept ;
-
Requires:
,sizeof ( To ) == sizeof ( From )
isis_trivially_copyable_v < To > true
,
isis_trivially_copyable_v < From > true
,
isis_standard_layout_v < To > true
,
isis_standard_layout_v < From > true
,
isis_pointer_v < To > && is_pointer_v < From > false
,
isis_member_pointer_v < To > && is_member_pointer_v < From > false
,
isis_member_object_pointer_v < To > && is_member_object_pointer_v < From > false
,
isis_member_function_pointer_v < To > && is_member_function_pointer_v < From > false
. -
Returns: an object of type
whose object representation is equal to the object representation ofTo
. If multiple object representations could represent the value representation ofFrom
, then it is unspecified whichFrom
value is returned. If no value representation corresponds toTo
's object representation then the returned value is unspecified.To
2.3. Feature testing
The
feature test macro should be added.
3. Appendix
The Standard’s [basic.types] section explicitly blesses
:
For any trivially copyable type
, if two pointers to
T point to distinct
T objects
T and
obj1 , where neither
obj2 nor
obj1 is a base-class subobject, if the underlying bytes (1.7) making up
obj2 are copied into
obj1 ,
obj2 shall subsequently hold the same value as
obj2 .
obj1 [Example:
— end example]T * t1p ; T * t2p ; // provided that t2p points to an initialized object ... std :: memcpy ( t1p , t2p , sizeof ( T )); // at this point, every subobject of trivially copyable type in *t1p contains // the same value as the corresponding subobject in *t2p
Whereas section [class.union] says:
In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time.
4. Acknowledgement
Thanks to Saam Barati, Jeffrey Yasskin, and Sam Benzaquen for their early review and suggested improvements.