1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10 
11 // [utility.underlying], to_underlying
12 // template <class T>
13 //     constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++2b
14 
15 #include <utility>
16 #include <cassert>
17 #include <limits>
18 
19 #include "test_macros.h"
20 
21 enum class e_default { a = 0, b = 1, c = 2 };
22 enum class e_ushort : unsigned short { d = 10, e = 25, f = 50 };
23 enum class e_longlong : long long {
24   low = std::numeric_limits<long long>::min(),
25   high = std::numeric_limits<long long>::max()
26 };
27 enum e_non_class { enum_a = 10, enum_b = 11, enum_c = 12 };
28 enum e_int : int {
29   enum_min = std::numeric_limits<int>::min(),
30   enum_max = std::numeric_limits<int>::max()
31 };
32 enum class e_bool : std::uint8_t { f = 0, t = 1 };
33 
34 struct WithBitfieldEnums {
35   e_default e1 : 3;
36   e_ushort e2 : 6;
37   e_bool e3 : 1;
38 };
39 
40 constexpr bool test() {
41   ASSERT_NOEXCEPT(std::to_underlying(e_default::a));
42   ASSERT_SAME_TYPE(int, decltype(std::to_underlying(e_default::a)));
43   ASSERT_SAME_TYPE(unsigned short, decltype(std::to_underlying(e_ushort::d)));
44   ASSERT_SAME_TYPE(long long, decltype(std::to_underlying(e_longlong::low)));
45   ASSERT_SAME_TYPE(int, decltype(std::to_underlying(enum_min)));
46   ASSERT_SAME_TYPE(int, decltype(std::to_underlying(enum_max)));
47 
48   assert(0 == std::to_underlying(e_default::a));
49   assert(1 == std::to_underlying(e_default::b));
50   assert(2 == std::to_underlying(e_default::c));
51 
52   assert(10 == std::to_underlying(e_ushort::d));
53   assert(25 == std::to_underlying(e_ushort::e));
54   assert(50 == std::to_underlying(e_ushort::f));
55 
56   // Check no truncating.
57   assert(std::numeric_limits<long long>::min() ==
58          std::to_underlying(e_longlong::low));
59   assert(std::numeric_limits<long long>::max() ==
60          std::to_underlying(e_longlong::high));
61 
62   assert(10 == std::to_underlying(enum_a));
63   assert(11 == std::to_underlying(enum_b));
64   assert(12 == std::to_underlying(enum_c));
65   assert(std::numeric_limits<int>::min() == std::to_underlying(enum_min));
66   assert(std::numeric_limits<int>::max() == std::to_underlying(enum_max));
67 
68   WithBitfieldEnums bf;
69   bf.e1 = static_cast<e_default>(3);
70   bf.e2 = e_ushort::e;
71   bf.e3 = e_bool::t;
72   assert(3 == std::to_underlying(bf.e1));
73   assert(25 == std::to_underlying(bf.e2));
74   assert(1 == std::to_underlying(bf.e3));
75 
76   return true;
77 }
78 
79 int main(int, char**) {
80   test();
81   static_assert(test());
82 
83   return 0;
84 }
85