Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaTemplate / constructor-template.cpp
bloba89dc60cfa3470757afb376d0f730c404cb31fba
1 // RUN: %clang_cc1 -fsyntax-only -verify=expected,precxx17 %std_cxx98-14 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
4 struct X0 { // expected-note {{candidate constructor (the implicit copy constructor) not viable}}
5 #if __cplusplus >= 201103L // C++11 or later
6 // expected-note@-2 {{candidate constructor (the implicit move constructor) not viable}}
7 #endif
8 X0(int); // expected-note{{candidate}}
9 template<typename T> X0(T); // expected-note {{candidate}}
10 template<typename T, typename U> X0(T*, U*); // expected-note {{candidate}}
12 // PR4761
13 template<typename T> X0() : f0(T::foo) {} // expected-note {{candidate}}
14 int f0;
17 void accept_X0(X0);
19 void test_X0(int i, float f) {
20 X0 x0a(i);
21 X0 x0b(f);
22 X0 x0c = i;
23 X0 x0d = f;
24 accept_X0(i);
25 accept_X0(&i);
26 accept_X0(f);
27 accept_X0(&f);
28 X0 x0e(&i, &f);
29 X0 x0f(&f, &i);
31 X0 x0g(f, &i); // expected-error{{no matching constructor}}
34 template<typename T>
35 struct X1 {
36 X1(const X1&);
37 template<typename U> X1(const X1<U>&);
40 template<typename T>
41 struct Outer {
42 typedef X1<T> A;
44 A alloc;
46 explicit Outer(const A& a) : alloc(a) { }
49 void test_X1(X1<int> xi) {
50 Outer<int> oi(xi);
51 Outer<float> of(xi);
54 // PR4655
55 template<class C> struct A {};
56 template <> struct A<int>{A(const A<int>&);};
57 struct B { A<int> x; B(B& a) : x(a.x) {} };
59 struct X2 {
60 X2(); // precxx17-note{{candidate constructor}}
61 X2(X2&); // precxx17-note {{candidate constructor}}
62 template<typename T> X2(T); // precxx17-note {{candidate template ignored: instantiation would take its own class type by value}}
65 X2 test(bool Cond, X2 x2) {
66 if (Cond)
67 return x2; // okay, uses copy constructor
69 return X2(); // precxx17-error{{no matching constructor}}
72 struct X3 {
73 template<typename T> X3(T);
76 template<> X3::X3(X3); // expected-error{{must pass its first argument by reference}}
78 struct X4 {
79 X4();
80 ~X4();
81 X4(X4&);
82 template<typename T> X4(const T&, int = 17);
85 X4 test_X4(bool Cond, X4 x4) {
86 X4 a(x4, 17); // okay, constructor template
87 X4 b(x4); // okay, copy constructor
88 return X4();
91 // Instantiation of a non-dependent use of a constructor
92 struct DefaultCtorHasDefaultArg {
93 explicit DefaultCtorHasDefaultArg(int i = 17);
96 template<typename T>
97 void default_ctor_inst() {
98 DefaultCtorHasDefaultArg def;
101 template void default_ctor_inst<int>();
103 template<typename T>
104 struct X5 {
105 X5();
106 X5(const T &);
109 struct X6 {
110 template<typename T> X6(T);
113 void test_X5_X6() {
114 X5<X6> tf;
115 X5<X6> tf2(tf);
118 namespace PR8182 {
119 struct foo {
120 foo();
121 template<class T> foo(T&);
123 private:
124 foo(const foo&);
127 void test_foo() {
128 foo f1;
129 foo f2(f1);
130 foo f3 = f1;
135 // Don't blow out the stack trying to call an illegal constructor
136 // instantiation. We intentionally allow implicit instantiations to
137 // exist, so make sure they're unusable.
138 namespace self_by_value {
139 template <class T, class U> struct A {
140 A() {}
141 A(const A<T,U> &o) {}
142 A(A<T,T> o) {}
145 void helper(A<int,float>);
147 void test1(A<int,int> a) {
148 helper(a);
150 void test2() {
151 helper(A<int,int>());
155 namespace self_by_value_2 {
156 template <class T, class U> struct A {
157 A() {} // precxx17-note {{not viable: requires 0 arguments}}
158 A(A<T,U> &o) {} // precxx17-note {{not viable: expects an lvalue}}
159 A(A<T,T> o) {} // precxx17-note {{ignored: instantiation takes its own class type by value}}
162 void helper_A(A<int,int>); // precxx17-note {{passing argument to parameter here}}
163 void test_A() {
164 helper_A(A<int,int>()); // precxx17-error {{no matching constructor}}
168 namespace self_by_value_3 {
169 template <class T, class U> struct A {
170 A() {}
171 A(A<T,U> &o) {}
172 A(A<T,T> o) {}
175 void helper_A(A<int,int>);
176 void test_A(A<int,int> b) {
177 helper_A(b);