[llvm] [ADT] Backport std::to_underlying from C++23 (PR #70681)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 30 11:29:02 PDT 2023


================
@@ -119,4 +119,20 @@ TEST(TransformTest, MoveTransformLlvm) {
   EXPECT_EQ(0u, MoveOnly::Destructions);
 }
 
+TEST(TransformTest, ToUnderlying) {
+  enum E { A1 = 0, B1 = -1 };
+  static_assert(llvm::to_underlying(A1) == 0);
+  static_assert(llvm::to_underlying(B1) == -1);
+
+  enum E2 : unsigned char { A2 = 0, B2 };
+  static_assert(std::is_same_v<unsigned char, decltype(llvm::to_underlying(A2))>);
+  static_assert(llvm::to_underlying(A2) == 0);
+  static_assert(llvm::to_underlying(B2) == 1);
+
+  enum class E3 { A3 = -1, B3 };
+  static_assert(std::is_same_v<int, decltype(llvm::to_underlying(E3::A3))>);
----------------
kuhar wrote:

Is this guaranteed to be an `int`? Maybe specify the `enum` type to make this clear -- it won't change test coverage.

https://github.com/llvm/llvm-project/pull/70681


More information about the llvm-commits mailing list