Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions llvm/include/llvm/ADT/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,13 @@ constexpr decltype(auto) makeVisitor(CallableTs &&...Callables) {
return detail::Visitor<CallableTs...>(std::forward<CallableTs>(Callables)...);
}

/// Backport of C++23 std::to_underlying.
Comment thread
kuhar marked this conversation as resolved.
Outdated
template <typename Enum>
[[nodiscard]] constexpr typename std::underlying_type_t<Enum>
to_underlying(Enum E) {
return static_cast<typename std::underlying_type_t<Enum>>(E);
Comment thread
kuhar marked this conversation as resolved.
Outdated
}

//===----------------------------------------------------------------------===//
// Extra additions to <algorithm>
//===----------------------------------------------------------------------===//
Expand Down
16 changes: 16 additions & 0 deletions llvm/unittests/ADT/STLExtrasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,4 +1332,20 @@ struct Bar {};
static_assert(is_incomplete_v<Foo>, "Foo is incomplete");
static_assert(!is_incomplete_v<Bar>, "Bar is defined");

TEST(STLExtrasTest, ToUnderlying) {
enum E { A1 = 0, B1 = -1 };
static_assert(to_underlying(A1) == 0);
static_assert(to_underlying(B1) == -1);

enum E2 : unsigned char { A2 = 0, B2 };
static_assert(std::is_same_v<unsigned char, decltype(to_underlying(A2))>);
static_assert(to_underlying(A2) == 0);
static_assert(to_underlying(B2) == 1);

enum class E3 { A3 = -1, B3 };
static_assert(std::is_same_v<int, decltype(to_underlying(E3::A3))>);
static_assert(to_underlying(E3::A3) == -1);
static_assert(to_underlying(E3::B3) == 0);
}

} // namespace