| 1 |
// Copyright (C) 2021-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do compile { target c++23 } } |
| 19 |
// { dg-add-options no_pch } |
| 20 |
|
| 21 |
#include <type_traits> |
| 22 |
|
| 23 |
#ifndef __cpp_lib_is_scoped_enum |
| 24 |
# error "Feature test macro for is_scoped_enum is missing in <type_traits>" |
| 25 |
#elif __cpp_lib_is_scoped_enum < 202011L |
| 26 |
# error "Feature test macro for is_scoped_enum has wrong value in <type_traits>" |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <testsuite_tr1.h> |
| 30 |
|
| 31 |
template<typename T> |
| 32 |
concept Is_scoped_enum |
| 33 |
= __gnu_test::test_category<std::is_scoped_enum, T>(true); |
| 34 |
|
| 35 |
struct Incomplete_struct; |
| 36 |
|
| 37 |
void |
| 38 |
test01() |
| 39 |
{ |
| 40 |
enum class E { e1, e2 }; |
| 41 |
static_assert( Is_scoped_enum<E> ); |
| 42 |
enum class Ec : char { e1, e2 }; |
| 43 |
static_assert( Is_scoped_enum<Ec> ); |
| 44 |
|
| 45 |
// negative tests |
| 46 |
enum U { u1, u2 }; |
| 47 |
static_assert( ! Is_scoped_enum<U> ); |
| 48 |
enum F : int { f1, f2 }; |
| 49 |
static_assert( ! Is_scoped_enum<F> ); |
| 50 |
static_assert( ! Is_scoped_enum<Incomplete_struct> ); |
| 51 |
struct S; |
| 52 |
static_assert( ! Is_scoped_enum<S> ); |
| 53 |
struct S { }; |
| 54 |
static_assert( ! Is_scoped_enum<S> ); |
| 55 |
|
| 56 |
static_assert( ! Is_scoped_enum<int> ); |
| 57 |
static_assert( ! Is_scoped_enum<int[]> ); |
| 58 |
static_assert( ! Is_scoped_enum<int[2]> ); |
| 59 |
static_assert( ! Is_scoped_enum<int[][2]> ); |
| 60 |
static_assert( ! Is_scoped_enum<int[2][3]> ); |
| 61 |
static_assert( ! Is_scoped_enum<int*> ); |
| 62 |
static_assert( ! Is_scoped_enum<int&> ); |
| 63 |
static_assert( ! Is_scoped_enum<int*&> ); |
| 64 |
static_assert( ! Is_scoped_enum<int()> ); |
| 65 |
static_assert( ! Is_scoped_enum<int(*)()> ); |
| 66 |
static_assert( ! Is_scoped_enum<int(&)()> ); |
| 67 |
} |
| 68 |
|
| 69 |
enum opaque_unscoped : short; |
| 70 |
enum class opaque_scoped; |
| 71 |
enum class opaque_scoped_with_base : long; |
| 72 |
|
| 73 |
static_assert( ! Is_scoped_enum<opaque_unscoped> ); |
| 74 |
static_assert( Is_scoped_enum<opaque_scoped> ); |
| 75 |
static_assert( Is_scoped_enum<opaque_scoped_with_base> ); |
| 76 |
|
| 77 |
void |
| 78 |
test02() |
| 79 |
{ |
| 80 |
enum unscoped { |
| 81 |
u_is_enum = std::is_enum_v<unscoped>, |
| 82 |
u_is_scoped = std::is_scoped_enum_v<unscoped>, |
| 83 |
}; |
| 84 |
static_assert( unscoped::u_is_enum ); |
| 85 |
static_assert( ! unscoped::u_is_scoped ); |
| 86 |
|
| 87 |
enum unscoped_fixed : char { |
| 88 |
uf_is_enum = std::is_enum_v<unscoped_fixed>, |
| 89 |
uf_is_scoped = std::is_scoped_enum_v<unscoped_fixed>, |
| 90 |
}; |
| 91 |
static_assert( unscoped_fixed::uf_is_enum); |
| 92 |
static_assert( ! unscoped_fixed::uf_is_scoped ); |
| 93 |
|
| 94 |
enum class scoped { |
| 95 |
is_enum = std::is_enum_v<scoped>, |
| 96 |
is_scoped = std::is_scoped_enum_v<scoped>, |
| 97 |
}; |
| 98 |
static_assert( (bool) scoped::is_enum ); |
| 99 |
static_assert( (bool) scoped::is_scoped ); |
| 100 |
} |