| 1 |
// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify -std=c++11 %s |
| 2 |
// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify -std=c++20 %s |
| 3 |
// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify -std=c++2c %s |
| 4 |
|
| 5 |
namespace std { |
| 6 |
typedef decltype(sizeof(int)) size_t; |
| 7 |
template <class _E> class initializer_list { |
| 8 |
const _E *__begin_; |
| 9 |
size_t __size_; |
| 10 |
|
| 11 |
constexpr initializer_list(const _E *__b, size_t __s) |
| 12 |
: __begin_(__b), __size_(__s) {} |
| 13 |
|
| 14 |
public: |
| 15 |
constexpr initializer_list() : __begin_(nullptr), __size_(0) {} |
| 16 |
}; |
| 17 |
} // namespace std |
| 18 |
|
| 19 |
template <typename T> |
| 20 |
struct Invalid { static_assert(false, "instantiated Invalid"); }; // #err-invalid |
| 21 |
|
| 22 |
template <typename T> |
| 23 |
int f(T a, Invalid<T> = {}); // #note-f |
| 24 |
|
| 25 |
// sanity check |
| 26 |
int e1 = f(0); |
| 27 |
//expected-error@#err-invalid {{static assertion failed: instantiated Invalid}} |
| 28 |
//expected-note@-2 {{in instantiation of default function argument expression for 'f<int>' required here}} |
| 29 |
//expected-note@#note-f {{in instantiation of template class 'Invalid<int>' requested here}} |
| 30 |
//expected-note@#note-f {{passing argument to parameter here}} |
| 31 |
|
| 32 |
int f(int); |
| 33 |
int ok1 = f(0); |
| 34 |
int e4 = f((const int&)(ok1)); |
| 35 |
|
| 36 |
int f(int, int = 0); |
| 37 |
int ok2 = f(0, 0); |
| 38 |
|
| 39 |
int e2 = f(0L); |
| 40 |
//expected-error@#err-invalid {{static assertion failed: instantiated Invalid}} |
| 41 |
//expected-note@-2 {{in instantiation of default function argument expression for 'f<long>' required here}} |
| 42 |
//expected-note@#note-f {{in instantiation of template class 'Invalid<long>' requested here}} |
| 43 |
//expected-note@#note-f {{passing argument to parameter here}} |
| 44 |
|
| 45 |
int f(long); |
| 46 |
int ok3 = f(0L); |
| 47 |
|
| 48 |
template <typename T> |
| 49 |
struct Invalid2 { static_assert(false, "instantiated Invalid2"); }; // #err-qualifiers |
| 50 |
|
| 51 |
template <typename T> |
| 52 |
int ref(T a, Invalid2<T> = {}); // expected-note 2{{here}} |
| 53 |
int ref(int&); |
| 54 |
int ref1 = ref(ok3); |
| 55 |
int ref2 = ref((const int&)ok3); // expected-note {{here}} |
| 56 |
//expected-error@#err-qualifiers {{static assertion failed: instantiated Invalid2}} |
| 57 |
|
| 58 |
|
| 59 |
template <typename T> |
| 60 |
int f_alias(T a, Invalid<T> = {}); |
| 61 |
using Alias = int; |
| 62 |
int f_alias(Alias); |
| 63 |
int ok4 = f_alias(0); |
| 64 |
|
| 65 |
#if __cplusplus >= 202002 |
| 66 |
|
| 67 |
struct Copyable { |
| 68 |
template <typename T> |
| 69 |
requires __is_constructible(Copyable, T) |
| 70 |
explicit Copyable(T op) noexcept; // #1 |
| 71 |
Copyable(const Copyable&) noexcept = default; // #2 |
| 72 |
}; |
| 73 |
static_assert(__is_constructible(Copyable, const Copyable&)); |
| 74 |
|
| 75 |
struct ImplicitlyCopyable { |
| 76 |
template <typename T> |
| 77 |
requires __is_constructible(ImplicitlyCopyable, T) |
| 78 |
explicit ImplicitlyCopyable(T op) = delete; // #1 |
| 79 |
}; |
| 80 |
static_assert(__is_constructible(ImplicitlyCopyable, const ImplicitlyCopyable&)); |
| 81 |
|
| 82 |
|
| 83 |
struct Movable { // #Movable |
| 84 |
template <typename T> |
| 85 |
requires __is_constructible(Movable, T) // #err-self-constraint-1 |
| 86 |
explicit Movable(T op) noexcept; // #Movable1 |
| 87 |
Movable(Movable&&) noexcept = default; // #Movable2 |
| 88 |
}; |
| 89 |
static_assert(__is_constructible(Movable, Movable&&)); |
| 90 |
static_assert(__is_constructible(Movable, const Movable&)); |
| 91 |
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(Movable, const Movable &)'}} \ |
| 92 |
// expected-error@-1 {{call to implicitly-deleted copy constructor of 'Movable'}} \ |
| 93 |
// expected-note@#Movable {{'Movable' defined here}} \ |
| 94 |
// expected-note@#Movable {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const Movable' for 1st argument}} \ |
| 95 |
// expected-note@#Movable2 {{copy constructor is implicitly deleted because 'Movable' has a user-declared move constructor}} \ |
| 96 |
// expected-note@#Movable2 {{candidate constructor not viable: no known conversion from 'int' to 'Movable' for 1st argument}} \ |
| 97 |
// expected-note@#Movable1 {{candidate template ignored: constraints not satisfied [with T = int]}} |
| 98 |
|
| 99 |
|
| 100 |
static_assert(__is_constructible(Movable, int)); |
| 101 |
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(Movable, int)'}} \ |
| 102 |
// expected-error@-1 {{no matching constructor for initialization of 'Movable'}} \ |
| 103 |
// expected-note@-1 2{{}} |
| 104 |
// expected-error@#err-self-constraint-1{{satisfaction of constraint '__is_constructible(Movable, T)' depends on itself}} |
| 105 |
// expected-note@#err-self-constraint-1 3{{}} |
| 106 |
// expected-note@#Movable {{'Movable' defined here}} |
| 107 |
|
| 108 |
template <typename T> |
| 109 |
struct Members { |
| 110 |
constexpr auto f(auto) { |
| 111 |
static_assert(false, ""); |
| 112 |
} |
| 113 |
constexpr auto f(int) { return 1; } |
| 114 |
constexpr auto f(int) requires true { return 2; } |
| 115 |
|
| 116 |
constexpr auto g(auto) { |
| 117 |
static_assert(false, "instantiated member"); //#err-qualified-member |
| 118 |
return 0; |
| 119 |
} |
| 120 |
constexpr auto g(int) & { return 1; } |
| 121 |
|
| 122 |
static constexpr auto s(auto) { |
| 123 |
static_assert(false, ""); |
| 124 |
} |
| 125 |
static constexpr auto s(int) { |
| 126 |
return 1; |
| 127 |
} |
| 128 |
static constexpr auto s(int) requires true { |
| 129 |
return 2; |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
static_assert(Members<int[1]>{}.f(0) == 2); |
| 134 |
static_assert(Members<int[2]>{}.g(0) == 0); |
| 135 |
// expected-error@#err-qualified-member {{static assertion failed: instantiated member}} \ |
| 136 |
// expected-note@-1{{in instantiation of function template specialization 'Members<int[2]>::g<int>' }} |
| 137 |
Members<int[3]> m1; |
| 138 |
static_assert(m1.g(0) == 1); |
| 139 |
static_assert(Members<int[3]>{}.s(0) == 2); |
| 140 |
|
| 141 |
|
| 142 |
namespace ConstructorInit{ |
| 143 |
struct S { |
| 144 |
template <typename T> |
| 145 |
S(T&&) {} |
| 146 |
}; |
| 147 |
struct Test { |
| 148 |
operator S() = delete; |
| 149 |
}; |
| 150 |
|
| 151 |
static_assert(__is_constructible(S, Test)); |
| 152 |
} |
| 153 |
|
| 154 |
namespace RefBinding { |
| 155 |
|
| 156 |
template <typename> struct remove_reference; |
| 157 |
template <typename _Tp> struct remove_reference<_Tp &> { |
| 158 |
using type = _Tp; |
| 159 |
}; |
| 160 |
template <typename _Tp> remove_reference<_Tp>::type move(_Tp &&); |
| 161 |
template <typename _Head> struct _Head_base { |
| 162 |
_Head_base(_Head &__h) : _M_head_impl(__h) {} |
| 163 |
template <typename _UHead> _Head_base(_UHead &&); |
| 164 |
_Head _M_head_impl; |
| 165 |
}; |
| 166 |
|
| 167 |
template <typename _Elements> void forward_as_tuple(_Elements &&) { |
| 168 |
_Head_base<_Elements &&>(_Elements{}); |
| 169 |
} |
| 170 |
struct StringRef { |
| 171 |
void operator[](const StringRef __k) { forward_as_tuple((move)(__k)); } |
| 172 |
}; |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
template <class> struct tuple {}; |
| 177 |
struct BonkersBananas { |
| 178 |
template <class T> operator T(); |
| 179 |
template <class = void> explicit operator tuple<int>() = delete; |
| 180 |
}; |
| 181 |
static_assert(!__is_constructible(tuple<int>, BonkersBananas)); |
| 182 |
|
| 183 |
namespace GH62096 { |
| 184 |
template <typename T> |
| 185 |
struct Oops { |
| 186 |
static_assert(sizeof(T) == 0); // #GH62096-err |
| 187 |
static constexpr bool value = true; |
| 188 |
}; |
| 189 |
|
| 190 |
template <class OPERATOR> |
| 191 |
concept Operator = Oops<OPERATOR>::value; // #GH62096-note1 |
| 192 |
|
| 193 |
template <Operator OP> void f(OP op); // // #GH62096-note2 |
| 194 |
void f(int); |
| 195 |
|
| 196 |
void g(int n) { f(n); } // OK |
| 197 |
void h(short n) { f(n); } |
| 198 |
// expected-error@#GH62096-err {{static assertion failed due to requirement 'sizeof(short) == 0'}} \ |
| 199 |
// expected-note@-1{{while substituting deduced template arguments}} \ |
| 200 |
// expected-note@-1{{while checking constraint satisfaction for template}} |
| 201 |
// expected-note@#GH62096-note1{{in instantiation}} |
| 202 |
// expected-note@#GH62096-note1{{while substituting template arguments into constraint expression here}} |
| 203 |
// expected-note@#GH62096-note2{{while checking the satisfaction of concept}} |
| 204 |
// expected-note@#GH62096-err {{expression evaluates}} |
| 205 |
} |
| 206 |
|
| 207 |
#endif |
| 208 |
|
| 209 |
template<typename ...Ts> |
| 210 |
struct t1 { |
| 211 |
}; |
| 212 |
struct t6 { |
| 213 |
template<typename T = int> |
| 214 |
operator t1<float>() { |
| 215 |
return {}; |
| 216 |
} |
| 217 |
}; |
| 218 |
|
| 219 |
void testT6() { |
| 220 |
t6 v6; |
| 221 |
v6.operator t1<float>(); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
using a = void(int &); |
| 226 |
template <typename c> void d(c &); |
| 227 |
void f(a); |
| 228 |
template <class> void f(bool j) { f(&d<int>); } |
| 229 |
|
| 230 |
struct InitListAreNotPerfect { |
| 231 |
InitListAreNotPerfect(int) = delete; |
| 232 |
template<class T> |
| 233 |
InitListAreNotPerfect(std::initializer_list<T>); |
| 234 |
}; |
| 235 |
InitListAreNotPerfect InitListAreNotPerfect_test({0}); |
| 236 |
struct InitListAreNotPerfectCpy { |
| 237 |
InitListAreNotPerfectCpy(); |
| 238 |
InitListAreNotPerfectCpy(const InitListAreNotPerfectCpy&); |
| 239 |
template <typename T> InitListAreNotPerfectCpy(std::initializer_list<T>); |
| 240 |
}; |
| 241 |
|
| 242 |
InitListAreNotPerfectCpy InitListAreNotPerfectCpy_test({InitListAreNotPerfectCpy{}}); |
| 243 |
|
| 244 |
namespace PointerToMemFunc { |
| 245 |
template <typename> |
| 246 |
class A; |
| 247 |
struct N { |
| 248 |
template <typename T> |
| 249 |
void f(T); |
| 250 |
}; |
| 251 |
template <typename T> |
| 252 |
struct E { |
| 253 |
template <class = A<int>> |
| 254 |
void g() = delete; |
| 255 |
void g(void (T::*)(char)); |
| 256 |
}; |
| 257 |
void f() { |
| 258 |
E<N> e; |
| 259 |
e.g(&N::f); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
#if __cplusplus >= 201402 |
| 264 |
namespace PointerToMemData { |
| 265 |
struct N { |
| 266 |
int field; |
| 267 |
}; |
| 268 |
template <typename It, typename T> |
| 269 |
struct B { |
| 270 |
B(It, T); |
| 271 |
template <typename It2> |
| 272 |
B(B<It2, T>); |
| 273 |
}; |
| 274 |
template <typename T> |
| 275 |
struct C { |
| 276 |
auto g() { return B<int, T>(0, T{}); } |
| 277 |
}; |
| 278 |
void f() { |
| 279 |
using T = decltype(C<decltype(&N::field)>{}.g()); |
| 280 |
} |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
#endif |
| 285 |
|
| 286 |
namespace GH147374 { |
| 287 |
|
| 288 |
struct String {}; |
| 289 |
template <typename T> void operator+(T, String &&) = delete; |
| 290 |
|
| 291 |
struct Bar { |
| 292 |
void operator+(String) const; // expected-note {{candidate function}} |
| 293 |
friend void operator+(Bar, String) {}; // expected-note {{candidate function}} |
| 294 |
}; |
| 295 |
|
| 296 |
struct Baz { |
| 297 |
void operator+(String); // expected-note {{candidate function}} |
| 298 |
friend void operator+(Baz, String) {}; // expected-note {{candidate function}} |
| 299 |
}; |
| 300 |
|
| 301 |
void test() { |
| 302 |
Bar a; |
| 303 |
String b; |
| 304 |
a + b; |
| 305 |
//expected-error@-1 {{use of overloaded operator '+' is ambiguous (with operand types 'Bar' and 'String')}} |
| 306 |
|
| 307 |
Baz z; |
| 308 |
z + b; |
| 309 |
//expected-error@-1 {{use of overloaded operator '+' is ambiguous (with operand types 'Baz' and 'String')}} |
| 310 |
} |
| 311 |
|
| 312 |
} |
| 313 |
|
| 314 |
namespace GH179118 { |
| 315 |
|
| 316 |
namespace std { |
| 317 |
|
| 318 |
template <bool __v> struct integral_constant { |
| 319 |
static constexpr bool value = __v; |
| 320 |
}; |
| 321 |
|
| 322 |
template <typename _Tp, typename... _Args> |
| 323 |
using __is_constructible_impl = |
| 324 |
integral_constant<__is_constructible(_Tp, _Args...)>; |
| 325 |
|
| 326 |
template <typename _Tp, typename... _Args> |
| 327 |
struct is_constructible : public std::__is_constructible_impl<_Tp, _Args...> {}; |
| 328 |
|
| 329 |
template <bool> struct _cond { |
| 330 |
template <typename Then, typename> using invoke = Then; |
| 331 |
}; |
| 332 |
template <> struct _cond<false> { |
| 333 |
template <typename, typename Else> using invoke = Else; |
| 334 |
}; |
| 335 |
|
| 336 |
template <bool If, typename Then, typename Else> |
| 337 |
using conditional_t = typename _cond<If>::template invoke<Then, Else>; |
| 338 |
|
| 339 |
template <bool, class _Tp = void> struct enable_if; |
| 340 |
template <class _Tp> struct enable_if<true, _Tp> { |
| 341 |
typedef _Tp type; |
| 342 |
}; |
| 343 |
template <bool _Bp, class _Tp = void> |
| 344 |
using enable_if_t = typename enable_if<_Bp, _Tp>::type; |
| 345 |
|
| 346 |
} // namespace std |
| 347 |
|
| 348 |
namespace base { |
| 349 |
|
| 350 |
template <typename...> struct disjunction {}; |
| 351 |
template <typename B1, typename... Bn> |
| 352 |
struct disjunction<B1, Bn...> |
| 353 |
: std::conditional_t<B1::value, B1, disjunction<>> {}; |
| 354 |
template <typename> class Optional; |
| 355 |
|
| 356 |
namespace internal { |
| 357 |
template <typename T, typename U> |
| 358 |
using IsConvertibleFromOptional = |
| 359 |
disjunction<std::is_constructible<T, Optional<U> &>>; |
| 360 |
template <typename T, typename U> |
| 361 |
using IsAssignableFromOptional = IsConvertibleFromOptional<T, U>; |
| 362 |
} // namespace internal |
| 363 |
|
| 364 |
template <typename T> class Optional { |
| 365 |
public: |
| 366 |
Optional(Optional &&); |
| 367 |
|
| 368 |
template <typename U, |
| 369 |
std::enable_if_t<internal::IsConvertibleFromOptional<T, U>::value> = |
| 370 |
false> |
| 371 |
Optional(Optional<U>); |
| 372 |
|
| 373 |
Optional(const Optional &); |
| 374 |
void operator=(Optional &&); |
| 375 |
|
| 376 |
template <typename U> |
| 377 |
std::enable_if_t<std::is_constructible<T, U>::value> operator=(U &&); |
| 378 |
|
| 379 |
template <typename U> |
| 380 |
std::enable_if_t<internal::IsAssignableFromOptional<T, U>::Optional> |
| 381 |
operator=(Optional<U>); |
| 382 |
}; |
| 383 |
|
| 384 |
} // namespace base |
| 385 |
|
| 386 |
struct LayoutUnit { |
| 387 |
template <typename IntegerType> LayoutUnit(IntegerType); |
| 388 |
}; |
| 389 |
|
| 390 |
static_assert( |
| 391 |
std::is_constructible<LayoutUnit, base::Optional<LayoutUnit> &>::value, ""); |
| 392 |
|
| 393 |
} |