blob: ee8475b3cdb81da8661e178a1287f38f09dbaff9 [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Charles Lie7cbb3e2015-11-17 20:25:05 +00002// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4
5struct 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 Gregor5ed5ae42009-08-21 18:42:58 +00009 X0(int); // expected-note{{candidate}}
John McCalld681c392009-12-16 08:11:27 +000010 template<typename T> X0(T); // expected-note {{candidate}}
11 template<typename T, typename U> X0(T*, U*); // expected-note {{candidate}}
Douglas Gregorc8c277a2009-08-24 11:57:43 +000012
13 // PR4761
John McCalld681c392009-12-16 08:11:27 +000014 template<typename T> X0() : f0(T::foo) {} // expected-note {{candidate}}
Douglas Gregorc8c277a2009-08-24 11:57:43 +000015 int f0;
Douglas Gregor5ed5ae42009-08-21 18:42:58 +000016};
17
18void accept_X0(X0);
19
20void 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 Gregor31fae892009-09-15 18:26:13 +000034
35template<typename T>
36struct X1 {
37 X1(const X1&);
38 template<typename U> X1(const X1<U>&);
39};
40
41template<typename T>
42struct Outer {
43 typedef X1<T> A;
44
45 A alloc;
46
47 explicit Outer(const A& a) : alloc(a) { }
48};
49
50void test_X1(X1<int> xi) {
51 Outer<int> oi(xi);
52 Outer<float> of(xi);
53}
Douglas Gregor45068b32009-09-15 21:14:05 +000054
55// PR4655
56template<class C> struct A {};
57template <> struct A<int>{A(const A<int>&);};
58struct B { A<int> x; B(B& a) : x(a.x) {} };
Douglas Gregor7861a802009-11-03 01:35:08 +000059
Douglas Gregorffe14e32009-11-14 01:20:54 +000060struct X2 {
John McCallfd0b2f82010-01-06 09:43:14 +000061 X2(); // expected-note{{candidate constructor}}
62 X2(X2&); // expected-note {{candidate constructor}}
John McCall578a1f82014-12-14 01:46:53 +000063 template<typename T> X2(T); // expected-note {{candidate template ignored: instantiation would take its own class type by value}}
Douglas Gregorffe14e32009-11-14 01:20:54 +000064};
65
66X2 test(bool Cond, X2 x2) {
67 if (Cond)
68 return x2; // okay, uses copy constructor
69
Douglas Gregore1314a62009-12-18 05:02:21 +000070 return X2(); // expected-error{{no matching constructor}}
Douglas Gregorffe14e32009-11-14 01:20:54 +000071}
72
73struct X3 {
74 template<typename T> X3(T);
75};
76
77template<> X3::X3(X3); // expected-error{{must pass its first argument by reference}}
78
79struct X4 {
Douglas Gregorbd6b17f2010-11-08 17:16:59 +000080 X4();
Douglas Gregorffe14e32009-11-14 01:20:54 +000081 ~X4();
Douglas Gregorbd6b17f2010-11-08 17:16:59 +000082 X4(X4&);
Douglas Gregorffe14e32009-11-14 01:20:54 +000083 template<typename T> X4(const T&, int = 17);
84};
85
86X4 test_X4(bool Cond, X4 x4) {
87 X4 a(x4, 17); // okay, constructor template
88 X4 b(x4); // okay, copy constructor
Douglas Gregorbd6b17f2010-11-08 17:16:59 +000089 return X4();
Douglas Gregorffe14e32009-11-14 01:20:54 +000090}
Douglas Gregord196a582009-12-14 19:27:10 +000091
92// Instantiation of a non-dependent use of a constructor
93struct DefaultCtorHasDefaultArg {
94 explicit DefaultCtorHasDefaultArg(int i = 17);
95};
96
97template<typename T>
98void default_ctor_inst() {
99 DefaultCtorHasDefaultArg def;
100}
101
102template void default_ctor_inst<int>();
Douglas Gregor5ab11652010-04-17 22:01:05 +0000103
104template<typename T>
105struct X5 {
106 X5();
107 X5(const T &);
108};
109
110struct X6 {
111 template<typename T> X6(T);
112};
113
114void test_X5_X6() {
115 X5<X6> tf;
116 X5<X6> tf2(tf);
117}
Douglas Gregorbd6b17f2010-11-08 17:16:59 +0000118
119namespace 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 McCall578a1f82014-12-14 01:46:53 +0000135
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
141namespace 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
158namespace 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
171namespace 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}