1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// template<class T, class U>
// concept same_as;
#include <concepts>
#include <type_traits>
struct S1 {};
struct S2 {
int i;
int& f();
double g(int x) const;
};
struct S3 {
int& r;
};
struct S4 {
int&& r;
};
struct S5 {
int* p;
};
class C1 {};
class C2 {
[[maybe_unused]] int i;
};
class C3 {
public:
int i;
};
template <class T1, class T2 = T1>
class C4 {
int t1;
int t2;
};
template <class T1, class T2 = T1>
class C5 {
[[maybe_unused]] T1 t1;
public:
T2 t2;
};
template <class T1, class T2 = T1>
class C6 {
public:
[[maybe_unused]] T1 t1;
[[maybe_unused]] T2 t2;
};
template <class T>
struct identity {
using type = T;
};
template <template <typename> class Modifier = identity>
void CheckSameAs() {
static_assert(
std::same_as<typename Modifier<int>::type, typename Modifier<int>::type>);
static_assert(
std::same_as<typename Modifier<S1>::type, typename Modifier<S1>::type>);
static_assert(
std::same_as<typename Modifier<S2>::type, typename Modifier<S2>::type>);
static_assert(
std::same_as<typename Modifier<S3>::type, typename Modifier<S3>::type>);
static_assert(
std::same_as<typename Modifier<S4>::type, typename Modifier<S4>::type>);
static_assert(
std::same_as<typename Modifier<S5>::type, typename Modifier<S5>::type>);
static_assert(
std::same_as<typename Modifier<C1>::type, typename Modifier<C1>::type>);
static_assert(
std::same_as<typename Modifier<C2>::type, typename Modifier<C2>::type>);
static_assert(
std::same_as<typename Modifier<C3>::type, typename Modifier<C3>::type>);
static_assert(std::same_as<typename Modifier<C4<int> >::type,
typename Modifier<C4<int> >::type>);
static_assert(std::same_as<typename Modifier<C4<int&> >::type,
typename Modifier<C4<int&> >::type>);
static_assert(std::same_as<typename Modifier<C4<int&&> >::type,
typename Modifier<C4<int&&> >::type>);
static_assert(std::same_as<typename Modifier<C5<int> >::type,
typename Modifier<C5<int> >::type>);
static_assert(std::same_as<typename Modifier<C5<int&> >::type,
typename Modifier<C5<int&> >::type>);
static_assert(std::same_as<typename Modifier<C5<int&&> >::type,
typename Modifier<C5<int&&> >::type>);
static_assert(std::same_as<typename Modifier<C6<int> >::type,
typename Modifier<C6<int> >::type>);
static_assert(std::same_as<typename Modifier<C6<int&> >::type,
typename Modifier<C6<int&> >::type>);
static_assert(std::same_as<typename Modifier<C6<int&&> >::type,
typename Modifier<C6<int&&> >::type>);
static_assert(std::same_as<typename Modifier<void>::type,
typename Modifier<void>::type>);
}
template <template <typename> class Modifier1,
template <typename> class Modifier2>
void CheckNotSameAs() {
static_assert(!std::same_as<typename Modifier1<int>::type,
typename Modifier2<int>::type>);
static_assert(!std::same_as<typename Modifier1<S1>::type,
typename Modifier2<S1>::type>);
static_assert(!std::same_as<typename Modifier1<S2>::type,
typename Modifier2<S2>::type>);
static_assert(!std::same_as<typename Modifier1<S3>::type,
typename Modifier2<S3>::type>);
static_assert(!std::same_as<typename Modifier1<S4>::type,
typename Modifier2<S4>::type>);
static_assert(!std::same_as<typename Modifier1<S5>::type,
typename Modifier2<S5>::type>);
static_assert(!std::same_as<typename Modifier1<C1>::type,
typename Modifier2<C1>::type>);
static_assert(!std::same_as<typename Modifier1<C2>::type,
typename Modifier2<C2>::type>);
static_assert(!std::same_as<typename Modifier1<C3>::type,
typename Modifier2<C3>::type>);
static_assert(!std::same_as<typename Modifier1<C4<int> >::type,
typename Modifier2<C4<int> >::type>);
static_assert(!std::same_as<typename Modifier1<C4<int&> >::type,
typename Modifier2<C4<int&> >::type>);
static_assert(!std::same_as<typename Modifier1<C4<int&&> >::type,
typename Modifier2<C4<int&&> >::type>);
static_assert(!std::same_as<typename Modifier1<C5<int> >::type,
typename Modifier2<C5<int> >::type>);
static_assert(!std::same_as<typename Modifier1<C5<int&> >::type,
typename Modifier2<C5<int&> >::type>);
static_assert(!std::same_as<typename Modifier1<C5<int&&> >::type,
typename Modifier2<C5<int&&> >::type>);
static_assert(!std::same_as<typename Modifier1<C6<int> >::type,
typename Modifier2<C6<int> >::type>);
static_assert(!std::same_as<typename Modifier1<C6<int&> >::type,
typename Modifier2<C6<int&> >::type>);
static_assert(!std::same_as<typename Modifier1<C6<int&&> >::type,
typename Modifier2<C6<int&&> >::type>);
}
// Checks subsumption works as intended
template <class T, class U>
requires std::same_as<T, U> void SubsumptionTest();
// clang-format off
template <class T, class U>
requires std::same_as<U, T> && true // NOLINT(readability-simplify-boolean-expr)
int SubsumptionTest();
// clang-format on
static_assert(std::same_as<int, decltype(SubsumptionTest<int, int>())>);
static_assert(std::same_as<int, decltype(SubsumptionTest<void, void>())>);
static_assert(
std::same_as<int, decltype(SubsumptionTest<int (*)(), int (*)()>())>);
static_assert(
std::same_as<
int, decltype(SubsumptionTest<double (&)(int), double (&)(int)>())>);
static_assert(
std::same_as<int, decltype(SubsumptionTest<int S2::*, int S2::*>())>);
static_assert(
std::same_as<int,
decltype(SubsumptionTest<int& (S2::*)(), int& (S2::*)()>())>);
int main(int, char**) {
{ // Checks std::same_as<T, T> is true
CheckSameAs();
// Checks std::same_as<T&, T&> is true
CheckSameAs<std::add_lvalue_reference>();
// Checks std::same_as<T&&, T&&> is true
CheckSameAs<std::add_rvalue_reference>();
// Checks std::same_as<const T, const T> is true
CheckSameAs<std::add_const>();
// Checks std::same_as<volatile T, volatile T> is true
CheckSameAs<std::add_volatile>();
// Checks std::same_as<const volatile T, const volatile T> is true
CheckSameAs<std::add_cv>();
// Checks std::same_as<T*, T*> is true
CheckSameAs<std::add_pointer>();
// Checks concrete types are identical
static_assert(std::same_as<void, void>);
using Void = void;
static_assert(std::same_as<void, Void>);
static_assert(std::same_as<int[1], int[1]>);
static_assert(std::same_as<int[2], int[2]>);
static_assert(std::same_as<int (*)(), int (*)()>);
static_assert(std::same_as<void (&)(), void (&)()>);
static_assert(std::same_as<S1& (*)(S1), S1& (*)(S1)>);
static_assert(std::same_as<C1& (&)(S1, int), C1& (&)(S1, int)>);
static_assert(std::same_as<int S2::*, int S2::*>);
static_assert(std::same_as<double S2::*, double S2::*>);
static_assert(std::same_as<int& (S2::*)(), int& (S2::*)()>);
static_assert(std::same_as<double& (S2::*)(int), double& (S2::*)(int)>);
}
{ // Checks that `T` and `T&` are distinct types
CheckNotSameAs<identity, std::add_lvalue_reference>();
CheckNotSameAs<std::add_lvalue_reference, identity>();
// Checks that `T` and `T&&` are distinct types
CheckNotSameAs<identity, std::add_rvalue_reference>();
CheckNotSameAs<std::add_rvalue_reference, identity>();
// Checks that `T` and `const T` are distinct types
CheckNotSameAs<identity, std::add_const>();
CheckNotSameAs<std::add_const, identity>();
// Checks that `T` and `volatile T` are distinct types
CheckNotSameAs<identity, std::add_volatile>();
CheckNotSameAs<std::add_volatile, identity>();
// Checks that `T` and `const volatile T` are distinct types
CheckNotSameAs<identity, std::add_cv>();
CheckNotSameAs<std::add_cv, identity>();
// Checks that `const T` and `volatile T` are distinct types
CheckNotSameAs<std::add_const, std::add_volatile>();
CheckNotSameAs<std::add_volatile, std::add_const>();
// Checks that `const T` and `const volatile T` are distinct types
CheckNotSameAs<std::add_const, std::add_cv>();
CheckNotSameAs<std::add_cv, std::add_const>();
// Checks that `volatile T` and `const volatile T` are distinct types
CheckNotSameAs<std::add_volatile, std::add_cv>();
CheckNotSameAs<std::add_cv, std::add_volatile>();
// Checks `T&` and `T&&` are distinct types
CheckNotSameAs<std::add_lvalue_reference, std::add_rvalue_reference>();
CheckNotSameAs<std::add_rvalue_reference, std::add_lvalue_reference>();
}
{ // Checks different type names are distinct types
static_assert(!std::same_as<S1, C1>);
static_assert(!std::same_as<C4<int>, C5<int> >);
static_assert(!std::same_as<C4<int>, C5<int> >);
static_assert(!std::same_as<C5<int, double>, C5<double, int> >);
static_assert(!std::same_as<int&, const int&>);
static_assert(!std::same_as<int&, volatile int&>);
static_assert(!std::same_as<int&, const volatile int&>);
static_assert(!std::same_as<int&&, const int&>);
static_assert(!std::same_as<int&&, volatile int&>);
static_assert(!std::same_as<int&&, const volatile int&>);
static_assert(!std::same_as<int&, const int&&>);
static_assert(!std::same_as<int&, volatile int&&>);
static_assert(!std::same_as<int&, const volatile int&&>);
static_assert(!std::same_as<int&&, const int&&>);
static_assert(!std::same_as<int&&, volatile int&&>);
static_assert(!std::same_as<int&&, const volatile int&&>);
static_assert(!std::same_as<void, int>);
static_assert(!std::same_as<int[1], int[2]>);
static_assert(!std::same_as<double[1], int[2]>);
static_assert(!std::same_as<int* (*)(), const int* (*)()>);
static_assert(!std::same_as<void (&)(), void (&)(S1)>);
static_assert(!std::same_as<S1 (*)(S1), S1& (*)(S1)>);
static_assert(!std::same_as<C3 (&)(int), C1& (&)(S1, int)>);
static_assert(!std::same_as<int S2::*, double S2::*>);
static_assert(!std::same_as<int& (S2::*)(), double& (S2::*)(int)>);
}
return 0;
}
|