Skip to content

Commit

Permalink
Use std::to_underlying with c++23 and later
Browse files Browse the repository at this point in the history
  • Loading branch information
jadhavrohit924 committed Sep 25, 2024
1 parent 8245330 commit 8653711
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/lib/support/TypeTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,21 @@

#include <type_traits>

#if __has_include(<utility>) // For C++23 and later, include <utility> if available
#include <utility> // Contains std::to_underlying
#endif

namespace chip {

#if __cplusplus >= 202300L

template <class T>
constexpr auto to_underlying(T e)
{
return std::to_underlying(e);
}

#else
/**
* @brief Implemented std::to_underlying introduced in C++23.
*/
Expand All @@ -37,6 +50,7 @@ constexpr std::underlying_type_t<T> to_underlying(T e)
static_assert(std::is_enum<T>::value, "to_underlying called to non-enum values.");
return static_cast<std::underlying_type_t<T>>(e);
}
#endif

/**
* @brief This template is not designed to be used directly. A common pattern to check the presence of a member of a class is:
Expand Down

0 comments on commit 8653711

Please sign in to comment.