Daniel Dunbar | 8fbe78f | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Charles Li | e7cbb3e | 2015-11-17 20:25:05 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s |
| 3 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s |
| 4 | |
| 5 | struct X0 { // expected-note {{candidate constructor (the implicit copy constructor) not viable}} |
| 6 | #if __cplusplus >= 201103L // C++11 or later |
| 7 | // expected-note@-2 {{candidate constructor (the implicit move constructor) not viable}} |
| 8 | #endif |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 9 | X0(int); // expected-note{{candidate}} |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 10 | template<typename T> X0(T); // expected-note {{candidate}} |
| 11 | template<typename T, typename U> X0(T*, U*); // expected-note {{candidate}} |
Douglas Gregor | c8c277a | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 12 | |
| 13 | // PR4761 |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 14 | template<typename T> X0() : f0(T::foo) {} // expected-note {{candidate}} |
Douglas Gregor | c8c277a | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 15 | int f0; |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 16 | }; |
| 17 | |
| 18 | void accept_X0(X0); |
| 19 | |
| 20 | void test_X0(int i, float f) { |
| 21 | X0 x0a(i); |
| 22 | X0 x0b(f); |
| 23 | X0 x0c = i; |
| 24 | X0 x0d = f; |
| 25 | accept_X0(i); |
| 26 | accept_X0(&i); |
| 27 | accept_X0(f); |
| 28 | accept_X0(&f); |
| 29 | X0 x0e(&i, &f); |
| 30 | X0 x0f(&f, &i); |
| 31 | |
| 32 | X0 x0g(f, &i); // expected-error{{no matching constructor}} |
| 33 | } |
Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 34 | |
| 35 | template<typename T> |
| 36 | struct X1 { |
| 37 | X1(const X1&); |
| 38 | template<typename U> X1(const X1<U>&); |
| 39 | }; |
| 40 | |
| 41 | template<typename T> |
| 42 | struct Outer { |
| 43 | typedef X1<T> A; |
| 44 | |
| 45 | A alloc; |
| 46 | |
| 47 | explicit Outer(const A& a) : alloc(a) { } |
| 48 | }; |
| 49 | |
| 50 | void test_X1(X1<int> xi) { |
| 51 | Outer<int> oi(xi); |
| 52 | Outer<float> of(xi); |
| 53 | } |
Douglas Gregor | 45068b3 | 2009-09-15 21:14:05 +0000 | [diff] [blame] | 54 | |
| 55 | // PR4655 |
| 56 | template<class C> struct A {}; |
| 57 | template <> struct A<int>{A(const A<int>&);}; |
| 58 | struct B { A<int> x; B(B& a) : x(a.x) {} }; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 59 | |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 60 | struct X2 { |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 61 | X2(); // expected-note{{candidate constructor}} |
| 62 | X2(X2&); // expected-note {{candidate constructor}} |
John McCall | 578a1f8 | 2014-12-14 01:46:53 +0000 | [diff] [blame] | 63 | template<typename T> X2(T); // expected-note {{candidate template ignored: instantiation would take its own class type by value}} |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | X2 test(bool Cond, X2 x2) { |
| 67 | if (Cond) |
| 68 | return x2; // okay, uses copy constructor |
| 69 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 70 | return X2(); // expected-error{{no matching constructor}} |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | struct X3 { |
| 74 | template<typename T> X3(T); |
| 75 | }; |
| 76 | |
| 77 | template<> X3::X3(X3); // expected-error{{must pass its first argument by reference}} |
| 78 | |
| 79 | struct X4 { |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 80 | X4(); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 81 | ~X4(); |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 82 | X4(X4&); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 83 | template<typename T> X4(const T&, int = 17); |
| 84 | }; |
| 85 | |
| 86 | X4 test_X4(bool Cond, X4 x4) { |
| 87 | X4 a(x4, 17); // okay, constructor template |
| 88 | X4 b(x4); // okay, copy constructor |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 89 | return X4(); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 90 | } |
Douglas Gregor | d196a58 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 91 | |
| 92 | // Instantiation of a non-dependent use of a constructor |
| 93 | struct DefaultCtorHasDefaultArg { |
| 94 | explicit DefaultCtorHasDefaultArg(int i = 17); |
| 95 | }; |
| 96 | |
| 97 | template<typename T> |
| 98 | void default_ctor_inst() { |
| 99 | DefaultCtorHasDefaultArg def; |
| 100 | } |
| 101 | |
| 102 | template void default_ctor_inst<int>(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 103 | |
| 104 | template<typename T> |
| 105 | struct X5 { |
| 106 | X5(); |
| 107 | X5(const T &); |
| 108 | }; |
| 109 | |
| 110 | struct X6 { |
| 111 | template<typename T> X6(T); |
| 112 | }; |
| 113 | |
| 114 | void test_X5_X6() { |
| 115 | X5<X6> tf; |
| 116 | X5<X6> tf2(tf); |
| 117 | } |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 118 | |
| 119 | namespace PR8182 { |
| 120 | struct foo { |
| 121 | foo(); |
| 122 | template<class T> foo(T&); |
| 123 | |
| 124 | private: |
| 125 | foo(const foo&); |
| 126 | }; |
| 127 | |
| 128 | void test_foo() { |
| 129 | foo f1; |
| 130 | foo f2(f1); |
| 131 | foo f3 = f1; |
| 132 | } |
| 133 | |
| 134 | } |
John McCall | 578a1f8 | 2014-12-14 01:46:53 +0000 | [diff] [blame] | 135 | |
| 136 | // Don't blow out the stack trying to call an illegal constructor |
| 137 | // instantiation. We intentionally allow implicit instantiations to |
| 138 | // exist, so make sure they're unusable. |
| 139 | // |
| 140 | // rdar://19199836 |
| 141 | namespace self_by_value { |
| 142 | template <class T, class U> struct A { |
| 143 | A() {} |
| 144 | A(const A<T,U> &o) {} |
| 145 | A(A<T,T> o) {} |
| 146 | }; |
| 147 | |
| 148 | void helper(A<int,float>); |
| 149 | |
| 150 | void test1(A<int,int> a) { |
| 151 | helper(a); |
| 152 | } |
| 153 | void test2() { |
| 154 | helper(A<int,int>()); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | namespace self_by_value_2 { |
| 159 | template <class T, class U> struct A { |
| 160 | A() {} // expected-note {{not viable: requires 0 arguments}} |
| 161 | A(A<T,U> &o) {} // expected-note {{not viable: expects an l-value}} |
| 162 | A(A<T,T> o) {} // expected-note {{ignored: instantiation takes its own class type by value}} |
| 163 | }; |
| 164 | |
| 165 | void helper_A(A<int,int>); // expected-note {{passing argument to parameter here}} |
| 166 | void test_A() { |
| 167 | helper_A(A<int,int>()); // expected-error {{no matching constructor}} |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | namespace self_by_value_3 { |
| 172 | template <class T, class U> struct A { |
| 173 | A() {} |
| 174 | A(A<T,U> &o) {} |
| 175 | A(A<T,T> o) {} |
| 176 | }; |
| 177 | |
| 178 | void helper_A(A<int,int>); |
| 179 | void test_A(A<int,int> b) { |
| 180 | helper_A(b); |
| 181 | } |
| 182 | } |