Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
static_if.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_MANIPULATION_STATIC_IF_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_MANIPULATION_STATIC_IF_HPP_
7
8namespace arene {
9namespace base {
10
11/// @brief A metaprogramming facility for selecting a branch statically without evaluating both branches apriori.
12///
13/// This has uses similar to @c std::conditional except that it allows for lazily evaluated metafunctions. Usage can
14/// lead to fewer template instantiations, which can have significant impact on compile times in low level facilities.
15///
16/// @note This is a rather advanced metaprogramming facility. If you aren't sure if you need it, you probably don't, and
17/// should prefer the more conventional @c std::conditional .
18///
19/// The principle that's followed is that instantiations of type templates are costly in terms of resource usage during
20/// compilation and have a tendency to add up surprisingly quickly, however, template aliases are close to free (you
21/// only pay for the substitution process of the template arguments). @c static_if minimizes resource usage at
22/// compile-time because regardless of how many times a developer uses @c static_if , with any combination of
23/// arguments, there are only two types: @c static_if<true> and @c static_if<false> . All branching is done with nested
24/// templates aliases.
25///
26/// In order to handle a variety of branch kinds, there are 4 different forms of "then else" corresponding to when none,
27/// one, or both branches take a lazily-evaluated metafunction. The naming convention used is that @c apply in the name
28/// corresponds to a parameter that is a metafunction. In other words:
29///
30/// then_else // No metafunctions
31/// then_apply_else // The "then" branch is a metafunction
32/// then_else_apply // The "else" branch is a metafunction
33/// then_apply_else_apply // Both "then" and "else" branches are metafunctions
34///
35/// Usage Example:
36/// @code {cpp}
37/// // Use a lock-free queue if T is trivially-copyable, otherwise a fallback.
38/// template <class T>
39/// using queue_type
40/// = typename static_if<std::is_trivially_copyable_v<T>> // The condition
41/// ::template then_apply_else_apply<
42/// lockfree_queue, // A lockfree queue template
43/// queue_with_locks, // A queue-with-locks template
44/// T // The parameters to pass to the chosen template
45/// >;
46/// @endcode
47template <bool V>
48struct static_if;
49
50/// @brief The definition when @c static_if gets a "true" condition
51// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
52template <>
53struct static_if<true> {
54 // Note:
55 // This is the "true" specialization, so all branches in this definition
56 // will take the "true" route.
57
58 /// @brief An alias that, itself, takes an "OnTrue" alias, an "OnFalse" alias, and a
59 /// pack of type parameters "...P"
60 ///
61 /// If condition were true (it is)
62 /// results in OnTrue<P..>
63 /// otherwise results in OnFalse<P...>
64 template <
65 template <class...>
66 class OnTrue, // Alias to apply if in the true branch (we are)
67 template <class...>
68 class, // Alias to apply if in the false
69 // branch (we're not)
70 class... P // Parameters to pass to the result alias
71 >
72 using then_apply_else_apply = OnTrue<P...>;
73
74 /// @brief If condition were true, apply an alias, otherwise result in OnFalse.
75 template <
76 template <class...>
77 class OnTrue, // Alias to apply if in the true branch (we are)
78 class, // Result if false (we're not false)
79 class... P // Parameters to pass to the result alias
80 >
81 using then_apply_else = OnTrue<P...>;
82
83 /// @brief If condition were true, result in OnTrue, otherwise apply an alias.
84 template <
85 class OnTrue, // Result if true (we are)
86 template <class...>
87 class, // Alias to apply if in the false
88 // branch (we're not)
89 class... // Parameters to pass to the result alias
90 >
91 using then_else_apply = OnTrue;
92
93 /// @brief If condition were true, result in OnTrue, otherwise result in OnFalse.
94 template <
95 class OnTrue, // Result if in the true branch (we are)
96 class // Result if in the false branch (we're not false)
97 >
98 using then_else = OnTrue;
99};
100// parasoft-end-suppress AUTOSAR-A2_7_3
101
102/// @brief The definition when static_if gets a "false" condition
103// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
104template <>
105struct static_if<false> {
106 // Note:
107 // This is the "false" specialization, so all branches in this definition
108 // will take the "false" route.
109
110 /// @brief An alias that, itself, takes an "OnTrue" alias, an "OnFalse" alias, and a
111 /// pack of type parameters "...P"
112 ///
113 /// If condition were true (it is not)
114 /// results in OnTrue<P..>
115 /// otherwise results in OnFalse<P...>
116 template <
117 template <class...>
118 class, // Alias to apply if in the true branch
119 // (we're not)
120 template <class...>
121 class OnFalse, // Alias to apply if in the false branch (we are)
122 class... P // Parameters to pass to the result alias
123 >
124 using then_apply_else_apply = OnFalse<P...>;
125
126 /// @brief If condition were true, apply an alias, otherwise result in OnFalse.
127 template <
128 template <class...>
129 class, // Alias to apply if in the true branch
130 // (we're not)
131 class OnFalse, // Result if false (we are)
132 class... // Parameters to pass to the result alias
133 >
134 using then_apply_else = OnFalse;
135
136 /// @brief If condition were true, result in OnTrue, otherwise apply an alias.
137 template <
138 class, // Result if true (we're not)
139 template <class...>
140 class OnFalse, // Alias to apply if in the false branch (we are)
141 class... P // Parameters to pass to the result alias
142 >
143 using then_else_apply = OnFalse<P...>;
144
145 /// @brief If condition were true, result in OnTrue, otherwise result in OnFalse.
146 template <
147 class, // Result if in the true branch (we're not are)
148 class OnFalse // Result if in the false branch (we are)
149 >
150 using then_else = OnFalse;
151};
152// parasoft-end-suppress AUTOSAR-A2_7_3
153
154} // namespace base
155} // namespace arene
156
157#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_MANIPULATION_STATIC_IF_HPP_
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10