313 lines · cpp
1// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify -std=c++11 %s2// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify -std=c++20 %s3// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify -std=c++2c %s4 5namespace 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 std18 19template <typename T>20struct Invalid { static_assert(false, "instantiated Invalid"); }; // #err-invalid21 22template <typename T>23int f(T a, Invalid<T> = {}); // #note-f24 25// sanity check26int 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 32int f(int);33int ok1 = f(0);34int e4 = f((const int&)(ok1));35 36int f(int, int = 0);37int ok2 = f(0, 0);38 39int 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 45int f(long);46int ok3 = f(0L);47 48template <typename T>49struct Invalid2 { static_assert(false, "instantiated Invalid2"); }; // #err-qualifiers50 51template <typename T>52int ref(T a, Invalid2<T> = {}); // expected-note 2{{here}}53int ref(int&);54int ref1 = ref(ok3);55int ref2 = ref((const int&)ok3); // expected-note {{here}}56//expected-error@#err-qualifiers {{static assertion failed: instantiated Invalid2}}57 58 59template <typename T>60int f_alias(T a, Invalid<T> = {});61using Alias = int;62int f_alias(Alias);63int ok4 = f_alias(0);64 65#if __cplusplus >= 20200266 67struct Copyable {68 template <typename T>69 requires __is_constructible(Copyable, T)70 explicit Copyable(T op) noexcept; // #171 Copyable(const Copyable&) noexcept = default; // #272};73static_assert(__is_constructible(Copyable, const Copyable&));74 75struct ImplicitlyCopyable {76 template <typename T>77 requires __is_constructible(ImplicitlyCopyable, T)78 explicit ImplicitlyCopyable(T op) = delete; // #179};80static_assert(__is_constructible(ImplicitlyCopyable, const ImplicitlyCopyable&));81 82 83struct Movable { // #Movable84 template <typename T>85 requires __is_constructible(Movable, T) // #err-self-constraint-186 explicit Movable(T op) noexcept; // #Movable187 Movable(Movable&&) noexcept = default; // #Movable288};89static_assert(__is_constructible(Movable, Movable&&));90static_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 100static_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 108template <typename T>109struct 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-member118 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 133static_assert(Members<int[1]>{}.f(0) == 2);134static_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>' }}137Members<int[3]> m1;138static_assert(m1.g(0) == 1);139static_assert(Members<int[3]>{}.s(0) == 2);140 141 142namespace ConstructorInit{143struct S {144 template <typename T>145 S(T&&) {}146};147struct Test {148 operator S() = delete;149};150 151static_assert(__is_constructible(S, Test));152}153 154namespace RefBinding {155 156template <typename> struct remove_reference;157template <typename _Tp> struct remove_reference<_Tp &> {158 using type = _Tp;159};160template <typename _Tp> remove_reference<_Tp>::type move(_Tp &&);161template <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 167template <typename _Elements> void forward_as_tuple(_Elements &&) {168 _Head_base<_Elements &&>(_Elements{});169}170struct StringRef {171 void operator[](const StringRef __k) { forward_as_tuple((move)(__k)); }172};173 174}175 176template <class> struct tuple {};177struct BonkersBananas {178 template <class T> operator T();179 template <class = void> explicit operator tuple<int>() = delete;180};181static_assert(!__is_constructible(tuple<int>, BonkersBananas));182 183namespace GH62096 {184template <typename T>185struct Oops {186 static_assert(sizeof(T) == 0); // #GH62096-err187 static constexpr bool value = true;188};189 190template <class OPERATOR>191concept Operator = Oops<OPERATOR>::value; // #GH62096-note1192 193template <Operator OP> void f(OP op); // // #GH62096-note2194void f(int);195 196void g(int n) { f(n); } // OK197void 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#endif208 209template<typename ...Ts>210struct t1 {211};212struct t6 {213 template<typename T = int>214 operator t1<float>() {215 return {};216 }217};218 219void testT6() {220 t6 v6;221 v6.operator t1<float>();222}223 224 225using a = void(int &);226template <typename c> void d(c &);227void f(a);228template <class> void f(bool j) { f(&d<int>); }229 230struct InitListAreNotPerfect {231 InitListAreNotPerfect(int) = delete;232 template<class T>233 InitListAreNotPerfect(std::initializer_list<T>);234};235InitListAreNotPerfect InitListAreNotPerfect_test({0});236struct InitListAreNotPerfectCpy {237 InitListAreNotPerfectCpy();238 InitListAreNotPerfectCpy(const InitListAreNotPerfectCpy&);239 template <typename T> InitListAreNotPerfectCpy(std::initializer_list<T>);240};241 242InitListAreNotPerfectCpy InitListAreNotPerfectCpy_test({InitListAreNotPerfectCpy{}});243 244namespace PointerToMemFunc {245template <typename>246class A;247struct N {248 template <typename T>249 void f(T);250};251template <typename T>252struct E {253 template <class = A<int>>254 void g() = delete;255 void g(void (T::*)(char));256};257void f() {258 E<N> e;259 e.g(&N::f);260}261}262 263#if __cplusplus >= 201402264namespace PointerToMemData {265struct N {266 int field;267};268template <typename It, typename T>269struct B {270 B(It, T);271 template <typename It2>272 B(B<It2, T>);273};274template <typename T>275struct C {276 auto g() { return B<int, T>(0, T{}); }277};278void f() {279 using T = decltype(C<decltype(&N::field)>{}.g());280}281 282}283 284#endif285 286namespace GH147374 {287 288struct String {};289template <typename T> void operator+(T, String &&) = delete;290 291struct Bar {292 void operator+(String) const; // expected-note {{candidate function}}293 friend void operator+(Bar, String) {}; // expected-note {{candidate function}}294};295 296struct Baz {297 void operator+(String); // expected-note {{candidate function}}298 friend void operator+(Baz, String) {}; // expected-note {{candidate function}}299};300 301void 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