Skip to content

Commit 08deed1

Browse files
committed
Add type traits
1 parent 1ec5e98 commit 08deed1

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

lib/public/StormByte/type_traits.hxx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ namespace StormByte {
220220
template<typename E>
221221
concept ScopedEnum = std::is_scoped_enum_v<E>;
222222

223+
/**
224+
* @brief Alias type giving the underlying integer type of an enum `E`.
225+
*
226+
* Usage:
227+
* enum class Foo : uint16_t { A };
228+
* Type::underlying_type<Foo> value = 0;
229+
*/
230+
template<typename E>
231+
requires Enum<E>
232+
using underlying_type = std::underlying_type_t<std::remove_cv_t<E>>;
233+
234+
/**
235+
* @brief Convert an enum value to its underlying integer representation.
236+
*/
237+
template<typename E>
238+
requires Enum<E>
239+
constexpr underlying_type<E> to_underlying(E e) noexcept {
240+
return static_cast<underlying_type<E>>(e);
241+
}
242+
223243
/**
224244
* @brief Concept to check if a type is a pointer.
225245
* @tparam T Type to check.

0 commit comments

Comments
 (0)