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. 32template <class _From, class _To> 33struct __is_always_bitcastable { 34 using _UnqualFrom = __remove_cv_t<_From>; 35 using _UnqualTo = __remove_cv_t<_To>; 36 37 static const bool value = 38 // First, the simple case -- `From` and `To` are the same object type. 39 (is_same<_UnqualFrom, _UnqualTo>::value && is_trivially_copyable<_UnqualFrom>::value) || 40 41 // Beyond the simple case, we say that one type is "always bit-castable" to another if: 42 // - (1) `From` and `To` have the same value representation, and in addition every possible value of `From` has 43 // a corresponding value in the `To` type (in other words, the set of values of `To` is a superset of the set of 44 // values of `From`); 45 // - (2) When the corresponding values are not the same value (as, for example, between an unsigned and a signed 46 // integer, where a large positive value of the unsigned integer corresponds to a negative value in the signed 47 // integer type), the value of `To` that results from a bitwise copy of `From` is the same what would be produced 48 // by the built-in assignment (if it were defined for the two types, to which there are minor exceptions, e.g. 49 // built-in arrays). 50 // 51 // In practice, that means: 52 // - all integral types (except `bool`, see below) -- that is, character types and `int` types, both signed and 53 // unsigned... 54 // - as well as arrays of such types... 55 // - ...that have the same size. 56 // 57 // Other trivially-copyable types can't be validly bit-cast outside of their own type: 58 // - floating-point types normally have different sizes and thus aren't bit-castable between each other (fails #1); 59 // - integral types and floating-point types use different representations, so for example bit-casting an integral 60 // `1` to `float` results in a very small less-than-one value, unlike built-in assignment that produces `1.0` 61 // (fails #2); 62 // - booleans normally use only a single bit of their object representation; bit-casting an integer to a boolean 63 // will result in a boolean object with an incorrect representation, which is undefined behavior (fails #2). 64 // Bit-casting from a boolean into an integer, however, is valid; 65 // - enumeration types may have different ranges of possible values (fails #1); 66 // - for pointers, it is not guaranteed that pointers to different types use the same set of values to represent 67 // addresses, and the conversion results are explicitly unspecified for types with different alignments 68 // (fails #1); 69 // - for structs and unions it is impossible to determine whether the set of values of one of them is a subset of 70 // the other (fails #1); 71 // - there is no need to consider `nullptr_t` for practical purposes. 72 ( 73 sizeof(_From) == sizeof(_To) && 74 is_integral<_From>::value && 75 is_integral<_To>::value && 76 !is_same<_UnqualTo, bool>::value 77 ); 78}; 79 80_LIBCPP_END_NAMESPACE_STD 81 82#endif // _LIBCPP___TYPE_TRAITS_IS_ALWAYS_BITCASTABLE_H 83