Skip to content

Commit 9a80cd0

Browse files
Add to_underlying backport (#85)
* Add to_underlying backport * Update include/kf/stl/backport/utility/detail/to_underlying.h Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent d975ed5 commit 9a80cd0

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
#include <utility>
3+
4+
#if !defined(__cpp_lib_to_underlying) // backport for older compilers
5+
6+
namespace kf
7+
{
8+
template<typename Enum>
9+
constexpr auto to_underlying(Enum e) noexcept
10+
{
11+
return static_cast<std::underlying_type_t<Enum>>(e);
12+
}
13+
}
14+
15+
#endif
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#pragma once
2+
#include "detail/to_underlying.h"

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ wdk_add_driver(kf-test WINVER NTDDI_WIN10 STL
4848
EncodingDetectorTest.cpp
4949
IntrusivePtrTest.cpp
5050
TextDetectorTest.cpp
51+
ToUnderlyingTest.cpp
5152
ScopeExitTest.cpp
5253
SingletonTest.cpp
5354
DoubleLinkedListTest.cpp

test/ToUnderlyingTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "pch.h"
2+
#undef __cpp_lib_to_underlying // pretend that the standard library doesn't provide to_underlying
3+
#include <kf/stl/backport/utility/detail/to_underlying.h>
4+
5+
enum class Color : uint8_t
6+
{
7+
Red = 1,
8+
Green = 2,
9+
Blue = 3,
10+
};
11+
12+
static_assert(kf::to_underlying(Color::Blue) == 3);
13+
static_assert(std::is_same_v<decltype(kf::to_underlying(Color::Blue)), uint8_t>);

0 commit comments

Comments
 (0)