blob: 87429001223acd29d75751d7c7cbf720d773e830 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Stephen Hines651f13c2014-04-23 16:59:28 -07002// RUN: %clang_cc1 -fsyntax-only -verify %s -fdelayed-template-parsing -DDELAYED_TEMPLATE_PARSING
3
4
Douglas Gregoradcac882008-12-01 23:54:00 +00005
6// Errors
7export class foo { }; // expected-error {{expected template}}
Douglas Gregord5a423b2009-09-25 18:43:00 +00008template x; // expected-error {{C++ requires a type specifier for all declarations}} \
9 // expected-error {{does not refer}}
Douglas Gregor7cdbc582009-07-22 23:48:44 +000010export template x; // expected-error {{expected '<' after 'template'}}
Douglas Gregor51ffb0c2009-11-25 18:55:14 +000011export template<class T> class x0; // expected-warning {{exported templates are unsupported}}
David Blaikieb031eab2012-04-06 23:33:59 +000012template < ; // expected-error {{expected template parameter}} \
Douglas Gregor99ea7342010-10-15 01:15:58 +000013// expected-error{{expected ',' or '>' in template-parameter-list}} \
14// expected-warning {{declaration does not declare anything}}
David Blaikieeb52f86a2012-04-09 16:37:11 +000015template <int +> struct x1; // expected-error {{expected ',' or '>' in template-parameter-list}}
16
17// verifies that we only walk to the ',' & still produce errors on the rest of the template parameters
18template <int +, T> struct x2; // expected-error {{expected ',' or '>' in template-parameter-list}} \
19 expected-error {{expected unqualified-id}}
20template<template<int+>> struct x3; // expected-error {{expected ',' or '>' in template-parameter-list}} \
21 expected-error {{template template parameter requires 'class' after the parameter list}}
Douglas Gregorf6b11852009-10-08 15:14:33 +000022template <template X> struct Err1; // expected-error {{expected '<' after 'template'}} \
23// expected-error{{extraneous}}
David Blaikie673720d2012-04-06 06:28:32 +000024template <template <typename> > struct Err2; // expected-error {{template template parameter requires 'class' after the parameter list}}
25template <template <typename> Foo> struct Err3; // expected-error {{template template parameter requires 'class' after the parameter list}}
Douglas Gregoradcac882008-12-01 23:54:00 +000026
27// Template function declarations
28template <typename T> void foo();
29template <typename T, typename U> void foo();
30
Douglas Gregor26236e82008-12-02 00:41:28 +000031// Template function definitions.
32template <typename T> void foo() { }
Douglas Gregoradcac882008-12-01 23:54:00 +000033
34// Template class (forward) declarations
35template <typename T> struct A;
36template <typename T, typename U> struct b;
37template <typename> struct C;
38template <typename, typename> struct D;
39
40// Forward declarations with default parameters?
Douglas Gregor4310f4e2009-02-16 22:38:20 +000041template <typename T = int> class X1;
42template <typename = int> class X2;
Douglas Gregoradcac882008-12-01 23:54:00 +000043
44// Forward declarations w/template template parameters
45template <template <typename> class T> class TTP1;
46template <template <typename> class> class TTP2;
Douglas Gregore53060f2009-06-25 22:08:12 +000047template <template <typename> class T = foo> class TTP3; // expected-error{{must be a class template}}
48template <template <typename> class = foo> class TTP3; // expected-error{{must be a class template}}
Douglas Gregoraaba5e32009-02-04 19:02:06 +000049template <template <typename X, typename Y> class T> class TTP5;
Douglas Gregoradcac882008-12-01 23:54:00 +000050
Douglas Gregor52591bf2009-06-24 00:54:41 +000051// Forward declarations with non-type params
Douglas Gregoradcac882008-12-01 23:54:00 +000052template <int> class NTP0;
53template <int N> class NTP1;
54template <int N = 5> class NTP2;
55template <int = 10> class NTP3;
Douglas Gregor4310f4e2009-02-16 22:38:20 +000056template <unsigned int N = 12u> class NTP4;
57template <unsigned int = 12u> class NTP5;
58template <unsigned = 15u> class NTP6;
59template <typename T, T Obj> class NTP7;
Douglas Gregoradcac882008-12-01 23:54:00 +000060
61// Template class declarations
62template <typename T> struct A { };
63template <typename T, typename U> struct B { };
64
Douglas Gregor72c3f312008-12-05 18:15:24 +000065// Template parameter shadowing
66template<typename T, // expected-note{{template parameter is declared here}}
Mike Stump1eb44332009-09-09 15:08:12 +000067 typename T> // expected-error{{declaration of 'T' shadows template parameter}}
Douglas Gregor72c3f312008-12-05 18:15:24 +000068 void shadow1();
69
70template<typename T> // expected-note{{template parameter is declared here}}
71void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}}
72
73template<typename T> // expected-note{{template parameter is declared here}}
74class T { // expected-error{{declaration of 'T' shadows template parameter}}
75};
76
77template<int Size> // expected-note{{template parameter is declared here}}
78void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}}
79
Douglas Gregorc19ee3e2009-06-17 23:37:01 +000080// <rdar://problem/6952203>
81template<typename T> // expected-note{{here}}
82struct shadow4 {
83 int T; // expected-error{{shadows}}
84};
85
86template<typename T> // expected-note{{here}}
87struct shadow5 {
88 int T(int, float); // expected-error{{shadows}}
89};
90
Richard Smithc7e863f2013-06-25 22:21:36 +000091template<typename T, // expected-note{{template parameter is declared here}}
92 T T> // expected-error{{declaration of 'T' shadows template parameter}}
93void shadow6();
94
95template<typename T, // expected-note{{template parameter is declared here}}
96 template<typename> class T> // expected-error{{declaration of 'T' shadows template parameter}}
97void shadow7();
98
99// PR8302
100template<template<typename> class T> struct shadow8 { // expected-note{{template parameter is declared here}}
101 template<template<typename> class T> struct inner; // expected-error{{declaration of 'T' shadows template parameter}}
102};
103
Douglas Gregor72c3f312008-12-05 18:15:24 +0000104// Non-type template parameters in scope
105template<int Size>
106void f(int& i) {
107 i = Size;
Stephen Hines651f13c2014-04-23 16:59:28 -0700108 #ifdef DELAYED_TEMPLATE_PARSING
109 Size = i;
110 #else
Douglas Gregor72c3f312008-12-05 18:15:24 +0000111 Size = i; // expected-error{{expression is not assignable}}
Stephen Hines651f13c2014-04-23 16:59:28 -0700112 #endif
Douglas Gregor72c3f312008-12-05 18:15:24 +0000113}
114
115template<typename T>
116const T& min(const T&, const T&);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +0000117
118void f2() {
119 int x;
120 A< typeof(x>1) > a;
121}
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000122
123
124// PR3844
125template <> struct S<int> { }; // expected-error{{explicit specialization of non-template struct 'S'}}
Rafael Espindolac4fb7ef2013-11-08 23:06:10 +0000126template <> union U<int> { }; // expected-error{{explicit specialization of non-template union 'U'}}
Douglas Gregor1df0ee92010-02-05 07:07:10 +0000127
128namespace PR6184 {
129 namespace N {
130 template <typename T>
131 void bar(typename T::x);
132 }
133
134 template <typename T>
135 void N::bar(typename T::x) { }
136}
Stephen Hines651f13c2014-04-23 16:59:28 -0700137
138// This PR occurred only in template parsing mode.
139namespace PR17637 {
140template <int>
141struct L {
142 template <typename T>
143 struct O {
144 template <typename U>
145 static void Fun(U);
146 };
147};
148
149template <int k>
150template <typename T>
151template <typename U>
152void L<k>::O<T>::Fun(U) {}
153
154void Instantiate() { L<0>::O<int>::Fun(0); }
155
156}
157
158namespace explicit_partial_specializations {
159typedef char (&oneT)[1];
160typedef char (&twoT)[2];
161typedef char (&threeT)[3];
162typedef char (&fourT)[4];
163typedef char (&fiveT)[5];
164typedef char (&sixT)[6];
165
166char one[1];
167char two[2];
168char three[3];
169char four[4];
170char five[5];
171char six[6];
172
173template<bool b> struct bool_ { typedef int type; };
174template<> struct bool_<false> { };
175
176#define XCAT(x,y) x ## y
177#define CAT(x,y) XCAT(x,y)
178#define sassert(_b_) bool_<(_b_)>::type CAT(var, __LINE__);
179
180
181template <int>
182struct L {
183 template <typename T>
184 struct O {
185 template <typename U>
186 static oneT Fun(U);
187
188 };
189};
190template <int k>
191template <typename T>
192template <typename U>
193oneT L<k>::O<T>::Fun(U) { return one; }
194
195template<>
196template<>
197template<typename U>
198oneT L<0>::O<char>::Fun(U) { return one; }
199
200
201void Instantiate() {
202 sassert(sizeof(L<0>::O<int>::Fun(0)) == sizeof(one));
203 sassert(sizeof(L<0>::O<char>::Fun(0)) == sizeof(one));
204}
205
206}