Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
traits.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a "File contains content related to variant traits."
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_VARIANT_TRAITS_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_VARIANT_TRAITS_HPP_
9
10// IWYU pragma: private, include "arene/base/variant.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13// parasoft-begin-suppress AUTOSAR-A16_2_2-a "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
14
15#include "arene/base/stdlib_choice/add_const.hpp"
16#include "arene/base/stdlib_choice/cstddef.hpp"
17#include "arene/base/stdlib_choice/integral_constant.hpp"
18#include "arene/base/stdlib_choice/numeric_limits.hpp"
21
22// parasoft-end-suppress AUTOSAR-A16_2_2-a "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
23
24namespace arene {
25namespace base {
26
27/// @brief Forward declaration of variant
28/// @tparam Ts the types in the variant
29template <typename... Ts>
30class variant;
31
32/// @brief Special value used as the return value of @c variant::index() when @c variant::valueless_by_exception() is
33/// true.
35
36/// @brief Declaration of @c variant_size_v. Default to @c variant_npos
37template <typename Variant>
39
40/// @brief Specialization of @c variant_size_v for @c variant.
41/// @tparam Ts the types in the variant
42template <typename... Ts>
43constexpr std::size_t variant_size_v<variant<Ts...>> = sizeof...(Ts);
44
45/// @brief Specialization of @c variant_size_v for const variant.
46/// @tparam Ts the types in the variant
47template <typename... Ts>
48constexpr std::size_t variant_size_v<variant<Ts...> const> = sizeof...(Ts);
49
50/// @brief Helper struct to get the size of a variant.
51/// @tparam Variant the variant type
52template <typename Variant>
53class variant_size : public std::integral_constant<std::size_t, variant_size_v<Variant>> {};
54
55/// @brief Traits to get the I-th type of a variant.
56/// @tparam I the index of the type
57/// @tparam T the variant type
58template <std::size_t I, class T>
60
61/// @brief Implementation of @c variant_alternative for variant with actual types.
62/// @tparam I the index of the type
63/// @tparam Ts the types that the variant can hold
64template <std::size_t I, class... Ts>
65struct variant_alternative<I, variant<Ts...>> {
66 /// @brief alternative type
67 using type = type_lists::at_t<type_list<Ts...>, I>;
68};
69
70/// @brief Implementation of variant_alternative for const variant with actual types.
71/// @tparam I the index of the type
72/// @tparam T the variant type
73template <std::size_t I, class T>
74struct variant_alternative<I, T const> {
75 public:
76 /// @brief const-qualified alternative type
78};
79
80/// @brief Helper type of variant_size variant_alternative.
81/// @tparam I the index of the type
82/// @tparam T the variant type
83template <std::size_t I, class T>
85
86} // namespace base
87} // namespace arene
88
89#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_VARIANT_TRAITS_HPP_
Helper struct to get the size of a variant.
Definition traits.hpp:53
Forward declaration of variant.
Definition variant.hpp:540
Add const to the provided type. The type member is an alias for the modified type.
Definition add_const.hpp:18
A helper class to represent a compile-time constant with the specified type and value....
Definition integral_constant.hpp:23
static constexpr auto max() noexcept -> T
The maximum finite value.
Definition numeric_limits.hpp:230
typename at< L0, Index >::type at_t
Get the n-th element from a type list.
Definition at.hpp:113
Definition accumulate.hpp:37
constexpr std::size_t variant_size_v
Declaration of variant_size_v. Default to variant_npos.
Definition traits.hpp:38
constexpr std::size_t variant_npos
Special value used as the return value of variant::index() when variant::valueless_by_exception() is ...
Definition traits.hpp:34
typename variant_alternative< I, T >::type variant_alternative_t
Helper type of variant_size variant_alternative.
Definition traits.hpp:84
Definition accumulate.hpp:36
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
an empty struct that is a list of types - cheaper to instantiate than tuple
Definition type_list.hpp:19
typename std::add_const< typename variant_alternative< I, T >::type >::type type
const-qualified alternative type
Definition traits.hpp:77
type_lists::at_t< type_list< Ts... >, I > type
alternative type
Definition traits.hpp:67
Traits to get the I-th type of a variant.
Definition traits.hpp:59