1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___TYPE_TRAITS_IS_ALWAYS_BITCASTABLE_H 10 #define _LIBCPP___TYPE_TRAITS_IS_ALWAYS_BITCASTABLE_H 11 12 #include <__config> 13 #include <__type_traits/integral_constant.h> 14 #include <__type_traits/is_integral.h> 15 #include <__type_traits/is_object.h> 16 #include <__type_traits/is_same.h> 17 #include <__type_traits/is_trivially_copyable.h> 18 #include <__type_traits/remove_cv.h> 19 20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 21 # pragma GCC system_header 22 #endif 23 24 _LIBCPP_BEGIN_NAMESPACE_STD 25 26 // Checks whether an object of type `From` can always be bit-cast to an object of type `To` and represent a valid value 27 // of type `To`. In other words, `From` and `To` have the same value representation and the set of values of `From` is 28 // a subset of the set of values of `To`. 29 // 30 // Note that types that cannot be assigned to each other using built-in assignment (e.g. arrays) might still be 31 // considered bit-castable. 32 template <class _From, class _To> 33 struct __is_always_bitcastable { 34 using _UnqualFrom = __remove_cv_t<_From>; 35 using _UnqualTo = __remove_cv_t<_To>; 36 37 // clang-format off 38 static const bool value = 39 // First, the simple case -- `From` and `To` are the same object type. 40 (is_same<_UnqualFrom, _UnqualTo>::value && is_trivially_copyable<_UnqualFrom>::value) || 41 42 // Beyond the simple case, we say that one type is "always bit-castable" to another if: 43 // - (1) `From` and `To` have the same value representation, and in addition every possible value of `From` has 44 // a corresponding value in the `To` type (in other words, the set of values of `To` is a superset of the set of 45 // values of `From`); 46 // - (2) When the corresponding values are not the same value (as, for example, between an unsigned and a signed 47 // integer, where a large positive value of the unsigned integer corresponds to a negative value in the signed 48 // integer type), the value of `To` that results from a bitwise copy of `From` is the same what would be 49 // produced by the built-in assignment (if it were defined for the two types, to which there are minor 50 // exceptions, e.g. built-in arrays). 51 // 52 // In practice, that means: 53 // - all integral types (except `bool`, see below) -- that is, character types and `int` types, both signed and 54 // unsigned... 55 // - as well as arrays of such types... 56 // - ...that have the same size. 57 // 58 // Other trivially-copyable types can't be validly bit-cast outside of their own type: 59 // - floating-point types normally have different sizes and thus aren't bit-castable between each other (fails 60 // #1); 61 // - integral types and floating-point types use different representations, so for example bit-casting an integral 62 // `1` to `float` results in a very small less-than-one value, unlike built-in assignment that produces `1.0` 63 // (fails #2); 64 // - booleans normally use only a single bit of their object representation; bit-casting an integer to a boolean 65 // will result in a boolean object with an incorrect representation, which is undefined behavior (fails #2). 66 // Bit-casting from a boolean into an integer, however, is valid; 67 // - enumeration types may have different ranges of possible values (fails #1); 68 // - for pointers, it is not guaranteed that pointers to different types use the same set of values to represent 69 // addresses, and the conversion results are explicitly unspecified for types with different alignments 70 // (fails #1); 71 // - for structs and unions it is impossible to determine whether the set of values of one of them is a subset of 72 // the other (fails #1); 73 // - there is no need to consider `nullptr_t` for practical purposes. 74 ( 75 sizeof(_From) == sizeof(_To) && 76 is_integral<_From>::value && 77 is_integral<_To>::value && 78 !is_same<_UnqualTo, bool>::value 79 ); 80 // clang-format on 81 }; 82 83 _LIBCPP_END_NAMESPACE_STD 84 85 #endif // _LIBCPP___TYPE_TRAITS_IS_ALWAYS_BITCASTABLE_H 86