1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
11 // [utility.underlying], to_underlying
13 // constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++23
20 #include "test_macros.h"
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()
28 enum e_non_class
{ enum_a
= 10, enum_b
= 11, enum_c
= 12 };
30 enum_min
= std::numeric_limits
<int>::min(),
31 enum_max
= std::numeric_limits
<int>::max()
33 enum class e_bool
: std::uint8_t { f
= 0, t
= 1 };
35 struct WithBitfieldEnums
{
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
)));
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
));
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
));
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
));
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
));
70 bf
.e1
= static_cast<e_default
>(3);
73 assert(3 == std::to_underlying(bf
.e1
));
74 assert(25 == std::to_underlying(bf
.e2
));
75 assert(1 == std::to_underlying(bf
.e3
));
80 int main(int, char**) {
82 static_assert(test());