blob: 040a932e972016abb942d546624cfeb9785b21ee [file] [log] [blame]
Stephen Hines176edba2014-12-01 14:53:08 -08001// RUN: %clang_cc1 -fsyntax-only -verify -fexceptions -fcxx-exceptions %s
2// RUN: %clang_cc1 -fsyntax-only -verify -fexceptions -fcxx-exceptions -std=c++11 %s
Douglas Gregord5a423b2009-09-25 18:43:00 +00003
4template void *; // expected-error{{expected unqualified-id}}
5
6template typedef void f0; // expected-error{{explicit instantiation of typedef}}
7
8int v0; // expected-note{{refers here}}
9template int v0; // expected-error{{does not refer}}
10
11template<typename T>
12struct X0 {
13 static T value;
14
15 T f0(T x) {
16 return x + 1; // expected-error{{invalid operands}}
Stephen Hines176edba2014-12-01 14:53:08 -080017 }
18 T *f0(T *, T *) { return T(); } // expected-warning 0-1 {{expression which evaluates to zero treated as a null pointer constant of type 'int *'}} expected-error 0-1 {{cannot initialize return object of type 'int *' with an rvalue of type 'int'}}
Larisse Voufo43847122013-07-19 23:00:19 +000019
Stephen Hines651f13c2014-04-23 16:59:28 -070020 template <typename U> T f0(T, U) { return T(); } // expected-note-re {{candidate template ignored: could not match 'int (int, U){{( __attribute__\(\(thiscall\)\))?}}' against 'int (int){{( __attribute__\(\(thiscall\)\))?}} const'}} \
Larisse Voufo43847122013-07-19 23:00:19 +000021 // expected-note {{candidate template ignored: could not match 'int' against 'int *'}}
Douglas Gregord5a423b2009-09-25 18:43:00 +000022};
23
Douglas Gregor558c0322009-10-14 23:41:34 +000024template<typename T>
25T X0<T>::value; // expected-error{{no matching constructor}}
26
Douglas Gregord5a423b2009-09-25 18:43:00 +000027template int X0<int>::value;
28
Stephen Hines176edba2014-12-01 14:53:08 -080029struct NotDefaultConstructible { // expected-note{{candidate constructor (the implicit copy constructor)}} expected-note 0-1 {{candidate constructor (the implicit move constructor)}}
John McCallb1622a12010-01-06 09:43:14 +000030 NotDefaultConstructible(int); // expected-note{{candidate constructor}}
Douglas Gregord5a423b2009-09-25 18:43:00 +000031};
32
Douglas Gregor558c0322009-10-14 23:41:34 +000033template NotDefaultConstructible X0<NotDefaultConstructible>::value; // expected-note{{instantiation}}
Douglas Gregord5a423b2009-09-25 18:43:00 +000034
35template int X0<int>::f0(int);
David Blaikie50800fc2012-08-08 17:33:31 +000036template int* X0<int>::f0(int*, int*); // expected-note{{in instantiation of member function 'X0<int>::f0' requested here}}
Douglas Gregord5a423b2009-09-25 18:43:00 +000037template int X0<int>::f0(int, float);
38
39template int X0<int>::f0(int) const; // expected-error{{does not refer}}
40template int* X0<int>::f0(int*, float*); // expected-error{{does not refer}}
41
42struct X1 { };
43typedef int X1::*MemPtr;
44
45template MemPtr X0<MemPtr>::f0(MemPtr); // expected-note{{requested here}}
46
47struct X2 {
48 int f0(int); // expected-note{{refers here}}
49
Douglas Gregor558c0322009-10-14 23:41:34 +000050 template<typename T> T f1(T) { return T(); }
51 template<typename T> T* f1(T*) { return 0; }
Douglas Gregord5a423b2009-09-25 18:43:00 +000052
Douglas Gregor558c0322009-10-14 23:41:34 +000053 template<typename T, typename U> void f2(T, U*) { } // expected-note{{candidate}}
54 template<typename T, typename U> void f2(T*, U) { } // expected-note{{candidate}}
Douglas Gregord5a423b2009-09-25 18:43:00 +000055};
56
57template int X2::f0(int); // expected-error{{not an instantiation}}
58
59template int *X2::f1(int *); // okay
60
61template void X2::f2(int *, int *); // expected-error{{ambiguous}}
Douglas Gregordb422df2009-09-25 21:45:23 +000062
Larisse Voufo43847122013-07-19 23:00:19 +000063template <typename T>
64void print_type() {} // expected-note {{candidate template ignored: could not match 'void ()' against 'void (float *)'}}
Douglas Gregordb422df2009-09-25 21:45:23 +000065
66template void print_type<int>();
67template void print_type<float>();
68
Larisse Voufo43847122013-07-19 23:00:19 +000069template <typename T>
70void print_type(T *) {} // expected-note {{candidate template ignored: could not match 'void (int *)' against 'void (float *)'}}
Douglas Gregordb422df2009-09-25 21:45:23 +000071
72template void print_type(int*);
73template void print_type<int>(float*); // expected-error{{does not refer}}
74
75void print_type(double*);
76template void print_type<double>(double*);
Douglas Gregorb2f81cf2009-10-01 23:51:25 +000077
78// PR5069
79template<int I> void foo0 (int (&)[I + 1]) { }
80template void foo0<2> (int (&)[3]);
Chandler Carruth291b4412010-02-13 10:17:50 +000081
82namespace explicit_instantiation_after_implicit_instantiation {
83 template <int I> struct X0 { static int x; };
84 template <int I> int X0<I>::x;
85 void test1() { (void)&X0<1>::x; }
86 template struct X0<1>;
87}
Douglas Gregor669eed82010-07-13 00:10:04 +000088
Douglas Gregor7306ebf2010-12-01 20:32:20 +000089template<typename> struct X3 { };
90inline template struct X3<int>; // expected-warning{{ignoring 'inline' keyword on explicit template instantiation}}
91static template struct X3<float>; // expected-warning{{ignoring 'static' keyword on explicit template instantiation}}
92
Sebastian Redld941fa42011-04-24 16:27:48 +000093namespace PR7622 {
Douglas Gregor669eed82010-07-13 00:10:04 +000094 template<typename,typename=int>
95 struct basic_streambuf;
96
Douglas Gregor669eed82010-07-13 00:10:04 +000097 template<typename,typename>
98 struct basic_streambuf{friend bob<>()}; // expected-error{{unknown type name 'bob'}} \
Larisse Voufo43847122013-07-19 23:00:19 +000099 // expected-error{{expected member name or ';' after declaration specifiers}}
Sebastian Redld941fa42011-04-24 16:27:48 +0000100 template struct basic_streambuf<int>;
101}
Argyrios Kyrtzidis92410572011-12-23 02:16:45 +0000102
103// Test that we do not crash.
104class TC1 {
105 class TC2 {
106 template // FIXME: error here.
107 void foo() { }
108 };
109};
Serge Pavlov313a9f42013-09-11 17:04:24 +0000110
111namespace PR8020 {
112 template <typename T> struct X { X() {} };
113 template<> struct X<int> { X(); };
114 template X<int>::X() {} // expected-error{{function cannot be defined in an explicit instantiation}}
115}
116
117namespace PR10086 {
118 template void foobar(int i) {} // expected-error{{function cannot be defined in an explicit instantiation}}
119 int func() {
120 foobar(5);
121 }
122}
Richard Smithd0629eb2013-09-27 20:14:12 +0000123
124namespace undefined_static_data_member {
125 template<typename T> struct A {
126 static int a; // expected-note {{here}}
127 template<typename U> static int b; // expected-note {{here}} expected-warning {{extension}}
128 };
129 struct B {
130 template<typename U> static int c; // expected-note {{here}} expected-warning {{extension}}
131 };
132
133 template int A<int>::a; // expected-error {{explicit instantiation of undefined static data member 'a' of class template 'undefined_static_data_member::A<int>'}}
134 template int A<int>::b<int>; // expected-error {{explicit instantiation of undefined variable template 'undefined_static_data_member::A<int>::b<int>'}}
135 template int B::c<int>; // expected-error {{explicit instantiation of undefined variable template 'undefined_static_data_member::B::c<int>'}}
136
137
138 template<typename T> struct C {
139 static int a;
140 template<typename U> static int b; // expected-warning {{extension}}
141 };
142 struct D {
143 template<typename U> static int c; // expected-warning {{extension}}
144 };
145 template<typename T> int C<T>::a;
146 template<typename T> template<typename U> int C<T>::b; // expected-warning {{extension}}
147 template<typename U> int D::c; // expected-warning {{extension}}
148
149 template int C<int>::a;
150 template int C<int>::b<int>;
151 template int D::c<int>;
152}
Stephen Hines176edba2014-12-01 14:53:08 -0800153
154// expected-note@+1 3-4 {{explicit instantiation refers here}}
155template <class T> void Foo(T i) throw(T) { throw i; }
156// expected-error@+1 {{exception specification in explicit instantiation does not match instantiated one}}
157template void Foo(int a) throw(char);
158// expected-error@+1 {{exception specification in explicit instantiation does not match instantiated one}}
159template void Foo(double a) throw();
160// expected-error@+1 1 {{exception specification in explicit instantiation does not match instantiated one}}
161template void Foo(long a) throw(long, char);
162template void Foo(float a);
163#if __cplusplus >= 201103L
164// expected-error@+1 0-1 {{exception specification in explicit instantiation does not match instantiated one}}
165template void Foo(double a) noexcept;
166#endif
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700167
168#if __cplusplus >= 201103L
169namespace PR21942 {
170template <int>
171struct A {
172 virtual void foo() final;
173};
174
175template <>
176void A<0>::foo() {} // expected-note{{overridden virtual function is here}}
177
178struct B : A<0> {
179 virtual void foo() override; // expected-error{{declaration of 'foo' overrides a 'final' function}}
180};
181}
182#endif