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++23 |
14 | |
15 | #include <utility> |
16 | #include <cassert> |
17 | #include <cstdint> |
18 | #include <limits> |
19 | |
20 | #include "test_macros.h" |
21 | |
22 | enum class e_default { a = 0, b = 1, c = 2 }; |
23 | enum class e_ushort : unsigned short { d = 10, e = 25, f = 50 }; |
24 | enum class e_longlong : long long { |
25 | low = std::numeric_limits<long long>::min(), |
26 | high = std::numeric_limits<long long>::max() |
27 | }; |
28 | enum e_non_class { enum_a = 10, enum_b = 11, enum_c = 12 }; |
29 | enum e_int : int { |
30 | enum_min = std::numeric_limits<int>::min(), |
31 | enum_max = std::numeric_limits<int>::max() |
32 | }; |
33 | enum class e_bool : std::uint8_t { f = 0, t = 1 }; |
34 | |
35 | struct WithBitfieldEnums { |
36 | e_default e1 : 3; |
37 | e_ushort e2 : 6; |
38 | e_bool e3 : 1; |
39 | }; |
40 | |
41 | constexpr bool test() { |
42 | ASSERT_NOEXCEPT(std::to_underlying(e_default::a)); |
43 | ASSERT_SAME_TYPE(int, decltype(std::to_underlying(e_default::a))); |
44 | ASSERT_SAME_TYPE(unsigned short, decltype(std::to_underlying(e_ushort::d))); |
45 | ASSERT_SAME_TYPE(long long, decltype(std::to_underlying(e_longlong::low))); |
46 | ASSERT_SAME_TYPE(int, decltype(std::to_underlying(enum_min))); |
47 | ASSERT_SAME_TYPE(int, decltype(std::to_underlying(enum_max))); |
48 | |
49 | assert(0 == std::to_underlying(e_default::a)); |
50 | assert(1 == std::to_underlying(e_default::b)); |
51 | assert(2 == std::to_underlying(e_default::c)); |
52 | |
53 | assert(10 == std::to_underlying(e_ushort::d)); |
54 | assert(25 == std::to_underlying(e_ushort::e)); |
55 | assert(50 == std::to_underlying(e_ushort::f)); |
56 | |
57 | // Check no truncating. |
58 | assert(std::numeric_limits<long long>::min() == |
59 | std::to_underlying(e_longlong::low)); |
60 | assert(std::numeric_limits<long long>::max() == |
61 | std::to_underlying(e_longlong::high)); |
62 | |
63 | assert(10 == std::to_underlying(enum_a)); |
64 | assert(11 == std::to_underlying(enum_b)); |
65 | assert(12 == std::to_underlying(enum_c)); |
66 | assert(std::numeric_limits<int>::min() == std::to_underlying(enum_min)); |
67 | assert(std::numeric_limits<int>::max() == std::to_underlying(enum_max)); |
68 | |
69 | WithBitfieldEnums bf; |
70 | bf.e1 = static_cast<e_default>(3); |
71 | bf.e2 = e_ushort::e; |
72 | bf.e3 = e_bool::t; |
73 | assert(3 == std::to_underlying(bf.e1)); |
74 | assert(25 == std::to_underlying(bf.e2)); |
75 | assert(1 == std::to_underlying(bf.e3)); |
76 | |
77 | return true; |
78 | } |
79 | |
80 | int main(int, char**) { |
81 | test(); |
82 | static_assert(test()); |
83 | |
84 | return 0; |
85 | } |
86 | |