Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
arithmetic_identities.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ARITHMETIC_IDENTITIES_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ARITHMETIC_IDENTITIES_HPP_
7
8// IWYU pragma: private, include "arene/base/math.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11namespace arene {
12namespace base {
13namespace numbers {
14
15/// @brief The additive identity of @c T
16/// @tparam T numeric type
17///
18/// Provides access to the additive identity of @c T through its @c static member @c value . This value is the additive
19/// identity; <c> a + zero<T>::value == a </c> and <c> zero<T>::value + a == a </c> are @c true for valid values of @c a
20/// of type @c T .
21///
22/// Users may specialize @c zero for cv-unqualified program-defined types. Such specializations shall have a @c static
23/// member @c value with type @c T whose value is the additive identity of @c T . A specialization is required if any of
24/// the following cases are true for the expression @c T{} :
25/// * is not well formed
26/// * does not match the required semantics
27/// * is not invocable within a constant expression
28///
29/// Because @c zero_v binds a reference to @c value , every specialization must also provide a namespace-scope
30/// definition of @c value (a separate out-of-line definition, as required for ODR use in C++14); otherwise naming
31/// @c zero_v for that type is ill-formed.
32///
33/// ~~~{.cpp}
34/// template <>
35/// struct arene::base::numbers::zero<my_type> {
36/// static constexpr my_type value{};
37/// };
38/// // Required for ODR use: zero_v<my_type> binds a reference to this member.
39/// constexpr my_type arene::base::numbers::zero<my_type>::value;
40/// ~~~
41template <typename T>
42struct zero {
43 /// @brief The additive identity value.
44 static constexpr T value{};
45};
46
47// Separate definition required for ODR use.
48template <typename T>
49constexpr T zero<T>::value;
50
51/// @brief @c constexpr reference to the additive identity value @c zero<T>::value .
52/// @tparam T numeric type
53///
54/// Binds a @c constexpr reference to @c zero<T>::value , so it is valid for every specialization, including one whose
55/// @c value is not usable in a constant expression. Reading the accessor @e within a constant expression additionally
56/// requires that @c zero<T>::value itself be usable in a constant expression.
57template <typename T>
58constexpr decltype(zero<T>::value) const& zero_v{zero<T>::value};
59
60/// @brief The multiplicative identity of @c T
61/// @tparam T numeric type
62///
63/// Provides access to the multiplicative identity of @c T through its @c static member @c value . This value is the
64/// multiplicative identity; <c> a * one<T>::value == a </c> and <c> one<T>::value * a == a </c> are @c true for valid
65/// values of @c a of type @c T .
66///
67/// Users may specialize @c one for cv-unqualified program-defined types. Such specializations shall have a @c static
68/// member @c value with type @c T whose value is the multiplicative identity of @c T . A specialization is required if
69/// any of the following cases are true for the expression @c T{1} :
70/// * is not well formed
71/// * does not match the required semantics
72/// * is not invocable within a constant expression
73///
74/// Because @c one_v binds a reference to @c value , every specialization must also provide a namespace-scope
75/// definition of @c value (a separate out-of-line definition, as required for ODR use in C++14); otherwise naming
76/// @c one_v for that type is ill-formed.
77///
78/// ~~~{.cpp}
79/// template <>
80/// struct arene::base::numbers::one<my_type> {
81/// static constexpr my_type value{1};
82/// };
83/// // Required for ODR use: one_v<my_type> binds a reference to this member.
84/// constexpr my_type arene::base::numbers::one<my_type>::value;
85/// ~~~
86template <typename T>
87struct one {
88 /// @brief The multiplicative identity value.
89 // parasoft-begin-suppress AUTOSAR-M5_0_5-a CERT_C-EXP39-a "Exact, intended int-to-T identity conversion"
90 static constexpr T value{1};
91 // parasoft-end-suppress AUTOSAR-M5_0_5-a CERT_C-EXP39-a
92};
93
94// Separate definition required for ODR use.
95template <typename T>
96constexpr T one<T>::value;
97
98/// @brief @c constexpr reference to the multiplicative identity value @c one<T>::value .
99/// @tparam T numeric type
100///
101/// Binds a @c constexpr reference to @c one<T>::value , so it is valid for every specialization, including one whose
102/// @c value is not usable in a constant expression. Reading the accessor @e within a constant expression additionally
103/// requires that @c one<T>::value itself be usable in a constant expression.
104template <typename T>
105constexpr decltype(one<T>::value) const& one_v{one<T>::value};
106
107} // namespace numbers
108} // namespace base
109} // namespace arene
110
111#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ARITHMETIC_IDENTITIES_HPP_
Definition arithmetic_identities.hpp:13
constexpr const & one_v
constexpr reference to the multiplicative identity value one<T>::value .
Definition arithmetic_identities.hpp:105
constexpr const & zero_v
constexpr reference to the additive identity value zero<T>::value .
Definition arithmetic_identities.hpp:58
Definition accumulate.hpp:37
Definition accumulate.hpp:36
The multiplicative identity of T.
Definition arithmetic_identities.hpp:87
static constexpr T value
The multiplicative identity value.
Definition arithmetic_identities.hpp:90
The additive identity of T.
Definition arithmetic_identities.hpp:42
static constexpr T value
The additive identity value.
Definition arithmetic_identities.hpp:44