blob: 8b2b12037b3c6abb49e243ce8f4e4790198421bf [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
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003// RUN: %clang_cc1 -fsyntax-only -verify -std=gnu++1z %s
Stephen Hines651f13c2014-04-23 16:59:28 -07004
5
Douglas Gregoradcac882008-12-01 23:54:00 +00006
7// Errors
8export class foo { }; // expected-error {{expected template}}
Douglas Gregord5a423b2009-09-25 18:43:00 +00009template x; // expected-error {{C++ requires a type specifier for all declarations}} \
10 // expected-error {{does not refer}}
Douglas Gregor7cdbc582009-07-22 23:48:44 +000011export template x; // expected-error {{expected '<' after 'template'}}
Douglas Gregor51ffb0c2009-11-25 18:55:14 +000012export template<class T> class x0; // expected-warning {{exported templates are unsupported}}
David Blaikieb031eab2012-04-06 23:33:59 +000013template < ; // expected-error {{expected template parameter}} \
Douglas Gregor99ea7342010-10-15 01:15:58 +000014// expected-error{{expected ',' or '>' in template-parameter-list}} \
15// expected-warning {{declaration does not declare anything}}
David Blaikieeb52f86a2012-04-09 16:37:11 +000016template <int +> struct x1; // expected-error {{expected ',' or '>' in template-parameter-list}}
17
18// verifies that we only walk to the ',' & still produce errors on the rest of the template parameters
19template <int +, T> struct x2; // expected-error {{expected ',' or '>' in template-parameter-list}} \
20 expected-error {{expected unqualified-id}}
21template<template<int+>> struct x3; // expected-error {{expected ',' or '>' in template-parameter-list}} \
22 expected-error {{template template parameter requires 'class' after the parameter list}}
Douglas Gregorf6b11852009-10-08 15:14:33 +000023template <template X> struct Err1; // expected-error {{expected '<' after 'template'}} \
24// expected-error{{extraneous}}
David Blaikie673720d2012-04-06 06:28:32 +000025template <template <typename> > struct Err2; // expected-error {{template template parameter requires 'class' after the parameter list}}
26template <template <typename> Foo> struct Err3; // expected-error {{template template parameter requires 'class' after the parameter list}}
Douglas Gregoradcac882008-12-01 23:54:00 +000027
Stephen Hinesc568f1e2014-07-21 00:47:37 -070028template <template <typename> typename Foo> struct Cxx1z;
29#if __cplusplus <= 201402L
30// expected-warning@-2 {{extension}}
31#endif
32
Douglas Gregoradcac882008-12-01 23:54:00 +000033// Template function declarations
34template <typename T> void foo();
35template <typename T, typename U> void foo();
36
Douglas Gregor26236e82008-12-02 00:41:28 +000037// Template function definitions.
38template <typename T> void foo() { }
Douglas Gregoradcac882008-12-01 23:54:00 +000039
40// Template class (forward) declarations
41template <typename T> struct A;
42template <typename T, typename U> struct b;
43template <typename> struct C;
44template <typename, typename> struct D;
45
46// Forward declarations with default parameters?
Douglas Gregor4310f4e2009-02-16 22:38:20 +000047template <typename T = int> class X1;
48template <typename = int> class X2;
Douglas Gregoradcac882008-12-01 23:54:00 +000049
50// Forward declarations w/template template parameters
51template <template <typename> class T> class TTP1;
52template <template <typename> class> class TTP2;
Douglas Gregore53060f2009-06-25 22:08:12 +000053template <template <typename> class T = foo> class TTP3; // expected-error{{must be a class template}}
54template <template <typename> class = foo> class TTP3; // expected-error{{must be a class template}}
Douglas Gregoraaba5e32009-02-04 19:02:06 +000055template <template <typename X, typename Y> class T> class TTP5;
Douglas Gregoradcac882008-12-01 23:54:00 +000056
Douglas Gregor52591bf2009-06-24 00:54:41 +000057// Forward declarations with non-type params
Douglas Gregoradcac882008-12-01 23:54:00 +000058template <int> class NTP0;
59template <int N> class NTP1;
60template <int N = 5> class NTP2;
61template <int = 10> class NTP3;
Douglas Gregor4310f4e2009-02-16 22:38:20 +000062template <unsigned int N = 12u> class NTP4;
63template <unsigned int = 12u> class NTP5;
64template <unsigned = 15u> class NTP6;
65template <typename T, T Obj> class NTP7;
Douglas Gregoradcac882008-12-01 23:54:00 +000066
67// Template class declarations
68template <typename T> struct A { };
69template <typename T, typename U> struct B { };
70
Douglas Gregor72c3f312008-12-05 18:15:24 +000071// Template parameter shadowing
72template<typename T, // expected-note{{template parameter is declared here}}
Mike Stump1eb44332009-09-09 15:08:12 +000073 typename T> // expected-error{{declaration of 'T' shadows template parameter}}
Douglas Gregor72c3f312008-12-05 18:15:24 +000074 void shadow1();
75
76template<typename T> // expected-note{{template parameter is declared here}}
77void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}}
78
79template<typename T> // expected-note{{template parameter is declared here}}
80class T { // expected-error{{declaration of 'T' shadows template parameter}}
81};
82
83template<int Size> // expected-note{{template parameter is declared here}}
84void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}}
85
Douglas Gregorc19ee3e2009-06-17 23:37:01 +000086// <rdar://problem/6952203>
87template<typename T> // expected-note{{here}}
88struct shadow4 {
89 int T; // expected-error{{shadows}}
90};
91
92template<typename T> // expected-note{{here}}
93struct shadow5 {
94 int T(int, float); // expected-error{{shadows}}
95};
96
Richard Smithc7e863f2013-06-25 22:21:36 +000097template<typename T, // expected-note{{template parameter is declared here}}
98 T T> // expected-error{{declaration of 'T' shadows template parameter}}
99void shadow6();
100
101template<typename T, // expected-note{{template parameter is declared here}}
102 template<typename> class T> // expected-error{{declaration of 'T' shadows template parameter}}
103void shadow7();
104
105// PR8302
106template<template<typename> class T> struct shadow8 { // expected-note{{template parameter is declared here}}
107 template<template<typename> class T> struct inner; // expected-error{{declaration of 'T' shadows template parameter}}
108};
109
Douglas Gregor72c3f312008-12-05 18:15:24 +0000110// Non-type template parameters in scope
111template<int Size>
112void f(int& i) {
113 i = Size;
Stephen Hines651f13c2014-04-23 16:59:28 -0700114 #ifdef DELAYED_TEMPLATE_PARSING
115 Size = i;
116 #else
Douglas Gregor72c3f312008-12-05 18:15:24 +0000117 Size = i; // expected-error{{expression is not assignable}}
Stephen Hines651f13c2014-04-23 16:59:28 -0700118 #endif
Douglas Gregor72c3f312008-12-05 18:15:24 +0000119}
120
121template<typename T>
122const T& min(const T&, const T&);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +0000123
124void f2() {
125 int x;
126 A< typeof(x>1) > a;
127}
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000128
129
130// PR3844
131template <> struct S<int> { }; // expected-error{{explicit specialization of non-template struct 'S'}}
Rafael Espindolac4fb7ef2013-11-08 23:06:10 +0000132template <> union U<int> { }; // expected-error{{explicit specialization of non-template union 'U'}}
Douglas Gregor1df0ee92010-02-05 07:07:10 +0000133
134namespace PR6184 {
135 namespace N {
136 template <typename T>
137 void bar(typename T::x);
138 }
139
140 template <typename T>
141 void N::bar(typename T::x) { }
142}
Stephen Hines651f13c2014-04-23 16:59:28 -0700143
144// This PR occurred only in template parsing mode.
145namespace PR17637 {
146template <int>
147struct L {
148 template <typename T>
149 struct O {
150 template <typename U>
151 static void Fun(U);
152 };
153};
154
155template <int k>
156template <typename T>
157template <typename U>
158void L<k>::O<T>::Fun(U) {}
159
160void Instantiate() { L<0>::O<int>::Fun(0); }
161
162}
163
164namespace explicit_partial_specializations {
165typedef char (&oneT)[1];
166typedef char (&twoT)[2];
167typedef char (&threeT)[3];
168typedef char (&fourT)[4];
169typedef char (&fiveT)[5];
170typedef char (&sixT)[6];
171
172char one[1];
173char two[2];
174char three[3];
175char four[4];
176char five[5];
177char six[6];
178
179template<bool b> struct bool_ { typedef int type; };
180template<> struct bool_<false> { };
181
182#define XCAT(x,y) x ## y
183#define CAT(x,y) XCAT(x,y)
184#define sassert(_b_) bool_<(_b_)>::type CAT(var, __LINE__);
185
186
187template <int>
188struct L {
189 template <typename T>
190 struct O {
191 template <typename U>
192 static oneT Fun(U);
193
194 };
195};
196template <int k>
197template <typename T>
198template <typename U>
199oneT L<k>::O<T>::Fun(U) { return one; }
200
201template<>
202template<>
203template<typename U>
204oneT L<0>::O<char>::Fun(U) { return one; }
205
206
207void Instantiate() {
208 sassert(sizeof(L<0>::O<int>::Fun(0)) == sizeof(one));
209 sassert(sizeof(L<0>::O<char>::Fun(0)) == sizeof(one));
210}
211
212}