Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
to_underlying.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_UTILITY_TO_UNDERLYING_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_TO_UNDERLYING_HPP_
7
8// IWYU pragma: private, include "arene/base/utility.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11#include "arene/base/stdlib_choice/underlying_type.hpp"
12
13namespace arene {
14namespace base {
15
16/// @brief This replicates a function that went into C++23 called @c std::to_underlying (https://wg21.link/P1682). It
17/// automatically processes and figures out the type of an enumeration value.
18///
19/// @remarks Usage sites benefit from this function in that they can never accidentally forget to update the code at
20/// several places when it comes to converting between an enumeration and its type. This can avoid problems with casting
21/// to the wrong type and getting bad integer promotions or signed -> unsigned (or vice-versa) conversions.
22/// @tparam T The type of the numeration for which to return the underlying value
23/// @param value The value of type @c T for which to retrieve the underlying value
24/// @return The integral value of the supplied enumeration value
25template <class T>
26constexpr auto to_underlying(T value) noexcept -> std::underlying_type_t<T> {
27 return static_cast<std::underlying_type_t<T>>(value);
28}
29
30} // namespace base
31} // namespace arene
32
33#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_TO_UNDERLYING_HPP_
Definition array_exceptions_disabled.cpp:11
constexpr auto to_underlying(T value) noexcept -> std::underlying_type_t< T >
This replicates a function that went into C++23 called std::to_underlying (https://wg21....
Definition to_underlying.hpp:26
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10