Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
conditional.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_TYPE_TRAITS_CONDITIONAL_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_CONDITIONAL_HPP_
7
8#include "arene/base/type_manipulation/static_if.hpp" // IWYU pragma: export
9
10namespace arene {
11namespace base {
12
13// parasoft-begin-suppress AUTOSAR-A2_7_3-a "False positive: declaration *is* preceeded by the @brief tag."
14/// @brief lazily select between two types based on a boolean condition
15/// @tparam B the boolean condition
16/// @tparam T the type selected when @p B is @c true
17/// @tparam F the type selected when @p B is @c false
18///
19/// Unlike @c std::conditional_t, this does not eagerly instantiate both
20/// branches. Only the selected type is used; the other remains untouched.
21///
22/// @note Serves as a drop-in replacement for @c std::conditional_t, backed by
23/// @c static_if for lazy evaluation.
24/// @note A corresponding @c conditional class template is intentionally not
25/// provided, to avoid unecessary class template instantiations. If the type is
26/// necessary, use @c std::conditional.
27template <bool B, class T, class F>
28using conditional_t = typename static_if<B>::template then_else<T, F>;
29// parasoft-end-suppress AUTOSAR-A2_7_3-a
30
31/// @brief an identity alias intended for use as a no-op branch in
32/// @c conditional_apply_t
33/// @tparam T the type to return as-is
34///
35/// When used as the @c OnTrue or @c OnFalse template parameter of
36/// @c conditional_apply_t, it passes the argument through unchanged.
37///
38/// @note @c std::type_identity_t is not suitable here because it is
39/// defined in terms of a class template (@c std::type_identity<T>::type),
40/// making it non-transparent. It cannot be redefined as a simple alias
41/// without breaking its common use as a type deduction barrier.
42template <class T>
43using conditional_identity_t = T;
44
45/// @brief lazily apply one of two template aliases based on a boolean
46/// condition
47/// @tparam B the boolean condition
48/// @tparam OnTrue template alias applied to @p Args when @p B is @c true
49/// @tparam OnFalse template alias applied to @p Args when @p B is @c false
50/// @tparam Args parameter pack forwarded to the selected template alias
51///
52/// Only the selected branch's template is instantiated with @p Args.
53/// The other branch's template is never applied, avoiding unnecessary
54/// instantiations and potential ill-formed types.
55///
56/// Example:
57/// @code
58/// // if T is void, apply std::add_pointer, otherwise return T as-is:
59/// using result = conditional_apply_t<
60/// std::is_void<T>::value,
61/// std::add_pointer_t,
62/// conditional_identity_t,
63/// T
64/// >;
65/// @endcode
66template < //
67 bool B,
68 template <class...>
69 class OnTrue,
70 template <class...>
71 class OnFalse,
72 class... Args>
74
75} // namespace base
76} // namespace arene
77#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_CONDITIONAL_HPP_
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10