xref: /llvm-project/clang/test/SemaTemplate/constructor-template.cpp (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -fsyntax-only -verify=expected,precxx17 %std_cxx98-14 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
3 
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}}
11 
12   // PR4761
X0X013   template<typename T> X0() : f0(T::foo) {} // expected-note {{candidate}}
14   int f0;
15 };
16 
17 void accept_X0(X0);
18 
test_X0(int i,float f)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);
30 
31   X0 x0g(f, &i); // expected-error{{no matching constructor}}
32 }
33 
34 template<typename T>
35 struct X1 {
36   X1(const X1&);
37   template<typename U> X1(const X1<U>&);
38 };
39 
40 template<typename T>
41 struct Outer {
42   typedef X1<T> A;
43 
44   A alloc;
45 
OuterOuter46   explicit Outer(const A& a) : alloc(a) { }
47 };
48 
test_X1(X1<int> xi)49 void test_X1(X1<int> xi) {
50   Outer<int> oi(xi);
51   Outer<float> of(xi);
52 }
53 
54 // PR4655
55 template<class C> struct A {};
56 template <> struct A<int>{A(const A<int>&);};
BB57 struct B { A<int> x; B(B& a) : x(a.x) {} };
58 
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}}
63 };
64 
test(bool Cond,X2 x2)65 X2 test(bool Cond, X2 x2) {
66   if (Cond)
67     return x2; // okay, uses copy constructor
68 
69   return X2(); // precxx17-error{{no matching constructor}}
70 }
71 
72 struct X3 {
73   template<typename T> X3(T);
74 };
75 
76 template<> X3::X3(X3); // expected-error{{must pass its first argument by reference}}
77 
78 struct X4 {
79   X4();
80   ~X4();
81   X4(X4&);
82   template<typename T> X4(const T&, int = 17);
83 };
84 
test_X4(bool Cond,X4 x4)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();
89 }
90 
91 // Instantiation of a non-dependent use of a constructor
92 struct DefaultCtorHasDefaultArg {
93   explicit DefaultCtorHasDefaultArg(int i = 17);
94 };
95 
96 template<typename T>
default_ctor_inst()97 void default_ctor_inst() {
98   DefaultCtorHasDefaultArg def;
99 }
100 
101 template void default_ctor_inst<int>();
102 
103 template<typename T>
104 struct X5 {
105   X5();
106   X5(const T &);
107 };
108 
109 struct X6 {
110   template<typename T> X6(T);
111 };
112 
test_X5_X6()113 void test_X5_X6() {
114   X5<X6> tf;
115   X5<X6> tf2(tf);
116 }
117 
118 namespace PR8182 {
119   struct foo {
120     foo();
121     template<class T> foo(T&);
122 
123   private:
124     foo(const foo&);
125   };
126 
test_foo()127   void test_foo() {
128     foo f1;
129     foo f2(f1);
130     foo f3 = f1;
131   }
132 
133 }
134 
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 {
Aself_by_value::A140     A() {}
Aself_by_value::A141     A(const A<T,U> &o) {}
Aself_by_value::A142     A(A<T,T> o) {}
143   };
144 
145   void helper(A<int,float>);
146 
test1(A<int,int> a)147   void test1(A<int,int> a) {
148     helper(a);
149   }
test2()150   void test2() {
151     helper(A<int,int>());
152   }
153 }
154 
155 namespace self_by_value_2 {
156   template <class T, class U> struct A {
Aself_by_value_2::A157     A() {} // precxx17-note {{not viable: requires 0 arguments}}
Aself_by_value_2::A158     A(A<T,U> &o) {} // precxx17-note {{not viable: expects an lvalue}}
Aself_by_value_2::A159     A(A<T,T> o) {} // precxx17-note {{ignored: instantiation takes its own class type by value}}
160   };
161 
162   void helper_A(A<int,int>); // precxx17-note {{passing argument to parameter here}}
test_A()163   void test_A() {
164     helper_A(A<int,int>()); // precxx17-error {{no matching constructor}}
165   }
166 }
167 
168 namespace self_by_value_3 {
169   template <class T, class U> struct A {
Aself_by_value_3::A170     A() {}
Aself_by_value_3::A171     A(A<T,U> &o) {}
Aself_by_value_3::A172     A(A<T,T> o) {}
173   };
174 
175   void helper_A(A<int,int>);
test_A(A<int,int> b)176   void test_A(A<int,int> b) {
177     helper_A(b);
178   }
179 }
180