Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
bit_cast.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines std::inner_product"
2
3// Copyright 2026, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_BIT_BIT_CAST_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_BIT_BIT_CAST_HPP_
9
10// IWYU pragma: private, include "arene/base/bit.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/compiler_support/platform_queries.hpp"
17#include "arene/base/compiler_support/preprocessor.hpp"
18#include "arene/base/constraints/constraints.hpp"
19#include "arene/base/stdlib_choice/enable_if.hpp"
20#include "arene/base/stdlib_choice/is_trivially_copyable.hpp"
21// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
22
23// NOLINTBEGIN(cppcoreguidelines-macro-usage) Selecting between constexpr and runtime implementations
24// parasoft-begin-suppress AUTOSAR-A16_0_1-d-2 "Conditional defines permitted by A16-0-1 Permit #2"
25// parasoft-begin-suppress AUTOSAR-A16_0_1-a-2 "Conditional defines permitted by A16-0-1 Permit #2"
26
27#if ARENE_IS_ON(ARENE_HAS_CONSTEXPR_BIT_CAST)
28#define ARENE_BIT_CAST_CONSTEXPR constexpr
29#include "arene/base/bit/detail/bit_cast_builtin.hpp" // IWYU pragma: export
30#else
31#define ARENE_BIT_CAST_CONSTEXPR
32#include "arene/base/bit/detail/bit_cast_memcpy.hpp" // IWYU pragma: export
33#endif
34
35// NOLINTEND(cppcoreguidelines-macro-usage)
36// parasoft-end-suppress AUTOSAR-A16_0_1-d-2
37// parasoft-end-suppress AUTOSAR-A16_0_1-a-2
38
39namespace arene {
40namespace base {
41namespace bit_cast_detail {
42
43// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
44
45/// @brief Function object implementing @c arene::base::bit_cast for a given destination type.
46/// @tparam To type to reinterpret as
47template <class To>
48class do_bit_cast {
49 public:
50 /// @brief Reinterpret the object representation of @c From as that of @c To.
51 /// @tparam From type to reinterpret from
52 /// @param from the source of bits for the return value
53 /// @return An object of type @c To whose value representation is copied from @c from.
54 template <
55 class From,
56 constraints<
57 std::enable_if_t<sizeof(From) == sizeof(To)>,
58 std::enable_if_t<std::is_trivially_copyable<From>::value>,
59 std::enable_if_t<std::is_trivially_copyable<To>::value && (sizeof(From*) != 0)>> = nullptr>
60 ARENE_BIT_CAST_CONSTEXPR auto operator()(From const& from) const noexcept -> To {
61 return ::arene::base::bit_cast_detail::bit_cast<To>(from);
62 }
63};
64
65// parasoft-end-suppress AUTOSAR-M2_10_1-a
66
67} // namespace bit_cast_detail
68
69/// @brief Reinterpret the object representation of one type as that of another
70/// @tparam To type to reinterpret as
71/// @tparam From type to reinterpret from
72/// @param from the source of bits for the return value
73///
74/// Obtain a value of type @c To by reinterpreting the object representation of
75/// @c From. Every bit in the value representation of the returned @c To object is
76/// equal to the corresponding bit in the object representation of @c from. The
77/// values of padding bits in the returned @c To object are unspecified.
78///
79/// If there is no value of type @c To corresponding to the value representation
80/// produced, the behavior is undefined. If there are multiple such values,
81/// which value is produced is unspecified.
82///
83/// A bit in the value representation of the result is indeterminate if it does
84/// not correspond to a bit in the value representation of @c From (i.e. it
85/// corresponds to a padding bit), or corresponds to a bit of an object that is
86/// not within its lifetime, or has an indeterminate value. For each bit in the
87/// value representation of the result that is indeterminate, the smallest
88/// object containing that bit has an indeterminate value; the behavior is
89/// undefined unless that object is of unsigned char type. The result does not
90/// otherwise contain any indeterminate values.
91///
92/// @return An object of type @c To whose value representation is as described
93/// above.
94///
95/// @note Requires:
96/// * <c> sizeof(To) == sizeof(From) </c> is @c true
97/// * <c> std::is_trivially_copyable<To>::value </c> is @c true
98/// * <c> std::is_trivially_copyable<From>::value </c> is @c true
99///
100/// @note @c bit_cast is usable in a constant expression only when the compiler
101/// provides @c __builtin_bit_cast . Query this via
102/// @c ARENE_IS_ON(ARENE_HAS_CONSTEXPR_BIT_CAST) . On toolchains without the
103/// builtin (e.g. GCC 8), @c bit_cast compiles but cannot appear in a
104/// @c constexpr context.
105template <class To>
106extern constexpr bit_cast_detail::do_bit_cast<To> bit_cast{};
107
108} // namespace base
109} // namespace arene
110
111// parasoft-end-suppress AUTOSAR-A7_1_5-a-2
112
113#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_BIT_BIT_CAST_HPP_
#define ARENE_BIT_CAST_CONSTEXPR
Definition bit_cast.hpp:31
Definition array_exceptions_disabled.cpp:11
constexpr bit_cast_detail::do_bit_cast< To > bit_cast
Reinterpret the object representation of one type as that of another.
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10