Skip to content

Commit 8653711

Browse files
Use std::to_underlying with c++23 and later
1 parent 8245330 commit 8653711

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/lib/support/TypeTraits.h

+14
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,21 @@
2626

2727
#include <type_traits>
2828

29+
#if __has_include(<utility>) // For C++23 and later, include <utility> if available
30+
#include <utility> // Contains std::to_underlying
31+
#endif
32+
2933
namespace chip {
3034

35+
#if __cplusplus >= 202300L
36+
37+
template <class T>
38+
constexpr auto to_underlying(T e)
39+
{
40+
return std::to_underlying(e);
41+
}
42+
43+
#else
3144
/**
3245
* @brief Implemented std::to_underlying introduced in C++23.
3346
*/
@@ -37,6 +50,7 @@ constexpr std::underlying_type_t<T> to_underlying(T e)
3750
static_assert(std::is_enum<T>::value, "to_underlying called to non-enum values.");
3851
return static_cast<std::underlying_type_t<T>>(e);
3952
}
53+
#endif
4054

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

0 commit comments

Comments
 (0)