|
| 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 |
| 10 | +// UNSUPPORTED: libcpp-no-concepts |
| 11 | + |
| 12 | +// template<class T> |
| 13 | +// struct indirectly_readable_traits; |
| 14 | + |
| 15 | +#include <iterator> |
| 16 | + |
| 17 | +#include <concepts> |
| 18 | +#include <memory> |
| 19 | +#include <optional> |
| 20 | +#include <string> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +// `value_type` and `element_type` member aliases aren't actually used to declare anytihng, so GCC |
| 24 | +// thinks they're completely unused. |
| 25 | +#pragma GCC diagnostic ignored "-Wunused-local-typedefs" |
| 26 | + |
| 27 | +// clang-format off |
| 28 | +template <class T> |
| 29 | +concept check_has_value_type = requires { |
| 30 | + typename std::indirectly_readable_traits<T>::value_type; |
| 31 | +}; |
| 32 | + |
| 33 | +template <class T, class Expected> |
| 34 | +concept check_value_type_matches = |
| 35 | + check_has_value_type<T> && |
| 36 | + std::same_as<typename std::indirectly_readable_traits<T>::value_type, Expected>; |
| 37 | +// clang-format on |
| 38 | + |
| 39 | +template <class T> |
| 40 | +constexpr bool check_pointer() { |
| 41 | + constexpr bool result = check_value_type_matches<T*, T>; |
| 42 | + static_assert(check_value_type_matches<T const*, T> == result); |
| 43 | + static_assert(check_value_type_matches<T volatile*, T> == result); |
| 44 | + static_assert(check_value_type_matches<T const volatile*, T> == result); |
| 45 | + |
| 46 | + static_assert(check_value_type_matches<T* const, T> == result); |
| 47 | + static_assert(check_value_type_matches<T const* const, T> == result); |
| 48 | + static_assert(check_value_type_matches<T volatile* const, T> == result); |
| 49 | + static_assert(check_value_type_matches<T const volatile* const, T> == result); |
| 50 | + |
| 51 | + return result; |
| 52 | +} |
| 53 | + |
| 54 | +static_assert(!check_pointer<void>()); |
| 55 | +static_assert(check_pointer<int>()); |
| 56 | +static_assert(check_pointer<long>()); |
| 57 | +static_assert(check_pointer<double>()); |
| 58 | +static_assert(check_pointer<double*>()); |
| 59 | + |
| 60 | +struct S {}; |
| 61 | +static_assert(check_pointer<S>()); |
| 62 | + |
| 63 | +template <class T> |
| 64 | +constexpr bool check_array() { |
| 65 | + constexpr bool result = check_value_type_matches<T[], T>; |
| 66 | + static_assert(check_value_type_matches<T const[], T> == result); |
| 67 | + static_assert(check_value_type_matches<T volatile[], T> == result); |
| 68 | + static_assert(check_value_type_matches<T const volatile[], T> == result); |
| 69 | + static_assert(check_value_type_matches<T[10], T> == result); |
| 70 | + static_assert(check_value_type_matches<T const[10], T> == result); |
| 71 | + static_assert(check_value_type_matches<T volatile[10], T> == result); |
| 72 | + static_assert(check_value_type_matches<T const volatile[10], T> == result); |
| 73 | + return result; |
| 74 | +} |
| 75 | + |
| 76 | +static_assert(check_array<int>()); |
| 77 | +static_assert(check_array<long>()); |
| 78 | +static_assert(check_array<double>()); |
| 79 | +static_assert(check_array<double*>()); |
| 80 | +static_assert(check_array<S>()); |
| 81 | + |
| 82 | +template <class T, class Expected> |
| 83 | +constexpr bool check_explicit_member() { |
| 84 | + constexpr bool result = check_value_type_matches<T, Expected>; |
| 85 | + static_assert(check_value_type_matches<T const, Expected> == result); |
| 86 | + return result; |
| 87 | +} |
| 88 | + |
| 89 | +struct has_value_type { |
| 90 | + using value_type = int; |
| 91 | +}; |
| 92 | +static_assert(check_explicit_member<has_value_type, int>()); |
| 93 | +static_assert(check_explicit_member<std::vector<int>::iterator, int>()); |
| 94 | + |
| 95 | +struct has_element_type { |
| 96 | + using element_type = S; |
| 97 | +}; |
| 98 | +static_assert(check_explicit_member<has_element_type, S>()); |
| 99 | + |
| 100 | +struct has_same_value_and_element_type { |
| 101 | + using value_type = int; |
| 102 | + using element_type = int; |
| 103 | +}; |
| 104 | +static_assert(check_explicit_member<has_same_value_and_element_type, int>()); |
| 105 | +static_assert(check_explicit_member<std::shared_ptr<long>, long>()); |
| 106 | +static_assert(check_explicit_member<std::shared_ptr<long const>, long>()); |
| 107 | + |
| 108 | +// clang-format off |
| 109 | +template<class T, class U> |
| 110 | +requires std::same_as<std::remove_cv_t<T>, std::remove_cv_t<U> > |
| 111 | +struct possibly_different_cv_qualifiers { |
| 112 | + using value_type = T; |
| 113 | + using element_type = U; |
| 114 | +}; |
| 115 | +// clang-format on |
| 116 | + |
| 117 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int, int>, int>()); |
| 118 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int, int const>, int>()); |
| 119 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int, int volatile>, int>()); |
| 120 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int, int const volatile>, int>()); |
| 121 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int const, int>, int>()); |
| 122 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int const, int const>, int>()); |
| 123 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int const, int volatile>, int>()); |
| 124 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int const, int const volatile>, int>()); |
| 125 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int volatile, int>, int>()); |
| 126 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int volatile, int const>, int>()); |
| 127 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int volatile, int volatile>, int>()); |
| 128 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int volatile, int const volatile>, int>()); |
| 129 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int const volatile, int>, int>()); |
| 130 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int const volatile, int const>, int>()); |
| 131 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int const volatile, int volatile>, int>()); |
| 132 | +static_assert(check_explicit_member<possibly_different_cv_qualifiers<int const volatile, int const volatile>, int>()); |
| 133 | + |
| 134 | +struct S2 {}; |
| 135 | +namespace std { |
| 136 | +template <> |
| 137 | +struct indirectly_readable_traits<S2> { |
| 138 | + using value_type = int; |
| 139 | +}; |
| 140 | +} // namespace std |
| 141 | +static_assert(check_value_type_matches<S2, int>); |
| 142 | +static_assert(check_value_type_matches<std::vector<int>, int>); |
| 143 | +static_assert(check_value_type_matches<std::vector<int>::iterator, int>); |
| 144 | +static_assert(check_value_type_matches<std::vector<int>::const_iterator, int>); |
| 145 | +static_assert(check_value_type_matches<std::istream_iterator<int>, int>); |
| 146 | +static_assert(check_value_type_matches<std::istreambuf_iterator<char>, char>); |
| 147 | +static_assert(check_value_type_matches<std::optional<int>, int>); |
| 148 | + |
| 149 | +template <class T> |
| 150 | +constexpr bool check_ref() { |
| 151 | + struct ref_value { |
| 152 | + using value_type = T&; |
| 153 | + }; |
| 154 | + constexpr bool result = check_has_value_type<ref_value>; |
| 155 | + |
| 156 | + struct ref_element { |
| 157 | + using element_type = T&; |
| 158 | + }; |
| 159 | + static_assert(check_has_value_type<ref_element> == result); |
| 160 | + |
| 161 | + return result; |
| 162 | +} |
| 163 | + |
| 164 | +static_assert(!check_ref<int>()); |
| 165 | +static_assert(!check_ref<S>()); |
| 166 | +static_assert(!check_ref<std::shared_ptr<long> >()); |
| 167 | + |
| 168 | +static_assert(!check_has_value_type<void>); |
| 169 | +static_assert(!check_has_value_type<std::nullptr_t>); |
| 170 | +static_assert(!check_has_value_type<int>); |
| 171 | +static_assert(!check_has_value_type<S>); |
| 172 | + |
| 173 | +struct has_different_value_and_element_type { |
| 174 | + using value_type = int; |
| 175 | + using element_type = long; |
| 176 | +}; |
| 177 | +static_assert(!check_has_value_type<has_different_value_and_element_type>); |
| 178 | + |
| 179 | +struct void_value { |
| 180 | + using value_type = void; |
| 181 | +}; |
| 182 | +static_assert(!check_has_value_type<void_value>); |
| 183 | + |
| 184 | +struct void_element { |
| 185 | + using element_type = void; |
| 186 | +}; |
| 187 | +static_assert(!check_has_value_type<void_element>); |
0 commit comments