xref: /minix3/external/bsd/llvm/dist/clang/test/Parser/cxx-template-decl.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // Errors
4*f4a2713aSLionel Sambuc export class foo { };   // expected-error {{expected template}}
5*f4a2713aSLionel Sambuc template  x;            // expected-error {{C++ requires a type specifier for all declarations}} \
6*f4a2713aSLionel Sambuc                         // expected-error {{does not refer}}
7*f4a2713aSLionel Sambuc export template x;      // expected-error {{expected '<' after 'template'}}
8*f4a2713aSLionel Sambuc export template<class T> class x0; // expected-warning {{exported templates are unsupported}}
9*f4a2713aSLionel Sambuc template < ;            // expected-error {{expected template parameter}} \
10*f4a2713aSLionel Sambuc // expected-error{{expected ',' or '>' in template-parameter-list}} \
11*f4a2713aSLionel Sambuc // expected-warning {{declaration does not declare anything}}
12*f4a2713aSLionel Sambuc template <int +> struct x1; // expected-error {{expected ',' or '>' in template-parameter-list}}
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc // verifies that we only walk to the ',' & still produce errors on the rest of the template parameters
15*f4a2713aSLionel Sambuc template <int +, T> struct x2; // expected-error {{expected ',' or '>' in template-parameter-list}} \
16*f4a2713aSLionel Sambuc                                 expected-error {{expected unqualified-id}}
17*f4a2713aSLionel Sambuc template<template<int+>> struct x3; // expected-error {{expected ',' or '>' in template-parameter-list}} \
18*f4a2713aSLionel Sambuc                                          expected-error {{template template parameter requires 'class' after the parameter list}}
19*f4a2713aSLionel Sambuc template <template X> struct Err1; // expected-error {{expected '<' after 'template'}} \
20*f4a2713aSLionel Sambuc // expected-error{{extraneous}}
21*f4a2713aSLionel Sambuc template <template <typename> > struct Err2;       // expected-error {{template template parameter requires 'class' after the parameter list}}
22*f4a2713aSLionel Sambuc template <template <typename> Foo> struct Err3;    // expected-error {{template template parameter requires 'class' after the parameter list}}
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc // Template function declarations
25*f4a2713aSLionel Sambuc template <typename T> void foo();
26*f4a2713aSLionel Sambuc template <typename T, typename U> void foo();
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc // Template function definitions.
29*f4a2713aSLionel Sambuc template <typename T> void foo() { }
30*f4a2713aSLionel Sambuc 
31*f4a2713aSLionel Sambuc // Template class (forward) declarations
32*f4a2713aSLionel Sambuc template <typename T> struct A;
33*f4a2713aSLionel Sambuc template <typename T, typename U> struct b;
34*f4a2713aSLionel Sambuc template <typename> struct C;
35*f4a2713aSLionel Sambuc template <typename, typename> struct D;
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc // Forward declarations with default parameters?
38*f4a2713aSLionel Sambuc template <typename T = int> class X1;
39*f4a2713aSLionel Sambuc template <typename = int> class X2;
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc // Forward declarations w/template template parameters
42*f4a2713aSLionel Sambuc template <template <typename> class T> class TTP1;
43*f4a2713aSLionel Sambuc template <template <typename> class> class TTP2;
44*f4a2713aSLionel Sambuc template <template <typename> class T = foo> class TTP3; // expected-error{{must be a class template}}
45*f4a2713aSLionel Sambuc template <template <typename> class = foo> class TTP3; // expected-error{{must be a class template}}
46*f4a2713aSLionel Sambuc template <template <typename X, typename Y> class T> class TTP5;
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc // Forward declarations with non-type params
49*f4a2713aSLionel Sambuc template <int> class NTP0;
50*f4a2713aSLionel Sambuc template <int N> class NTP1;
51*f4a2713aSLionel Sambuc template <int N = 5> class NTP2;
52*f4a2713aSLionel Sambuc template <int = 10> class NTP3;
53*f4a2713aSLionel Sambuc template <unsigned int N = 12u> class NTP4;
54*f4a2713aSLionel Sambuc template <unsigned int = 12u> class NTP5;
55*f4a2713aSLionel Sambuc template <unsigned = 15u> class NTP6;
56*f4a2713aSLionel Sambuc template <typename T, T Obj> class NTP7;
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc // Template class declarations
59*f4a2713aSLionel Sambuc template <typename T> struct A { };
60*f4a2713aSLionel Sambuc template <typename T, typename U> struct B { };
61*f4a2713aSLionel Sambuc 
62*f4a2713aSLionel Sambuc // Template parameter shadowing
63*f4a2713aSLionel Sambuc template<typename T, // expected-note{{template parameter is declared here}}
64*f4a2713aSLionel Sambuc          typename T> // expected-error{{declaration of 'T' shadows template parameter}}
65*f4a2713aSLionel Sambuc   void shadow1();
66*f4a2713aSLionel Sambuc 
67*f4a2713aSLionel Sambuc template<typename T> // expected-note{{template parameter is declared here}}
68*f4a2713aSLionel Sambuc void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}}
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc template<typename T> // expected-note{{template parameter is declared here}}
71*f4a2713aSLionel Sambuc class T { // expected-error{{declaration of 'T' shadows template parameter}}
72*f4a2713aSLionel Sambuc };
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc template<int Size> // expected-note{{template parameter is declared here}}
75*f4a2713aSLionel Sambuc void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}}
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc // <rdar://problem/6952203>
78*f4a2713aSLionel Sambuc template<typename T> // expected-note{{here}}
79*f4a2713aSLionel Sambuc struct shadow4 {
80*f4a2713aSLionel Sambuc   int T; // expected-error{{shadows}}
81*f4a2713aSLionel Sambuc };
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc template<typename T> // expected-note{{here}}
84*f4a2713aSLionel Sambuc struct shadow5 {
85*f4a2713aSLionel Sambuc   int T(int, float); // expected-error{{shadows}}
86*f4a2713aSLionel Sambuc };
87*f4a2713aSLionel Sambuc 
88*f4a2713aSLionel Sambuc template<typename T, // expected-note{{template parameter is declared here}}
89*f4a2713aSLionel Sambuc          T T> // expected-error{{declaration of 'T' shadows template parameter}}
90*f4a2713aSLionel Sambuc void shadow6();
91*f4a2713aSLionel Sambuc 
92*f4a2713aSLionel Sambuc template<typename T, // expected-note{{template parameter is declared here}}
93*f4a2713aSLionel Sambuc          template<typename> class T> // expected-error{{declaration of 'T' shadows template parameter}}
94*f4a2713aSLionel Sambuc void shadow7();
95*f4a2713aSLionel Sambuc 
96*f4a2713aSLionel Sambuc // PR8302
97*f4a2713aSLionel Sambuc template<template<typename> class T> struct shadow8 { // expected-note{{template parameter is declared here}}
98*f4a2713aSLionel Sambuc   template<template<typename> class T> struct inner; // expected-error{{declaration of 'T' shadows template parameter}}
99*f4a2713aSLionel Sambuc };
100*f4a2713aSLionel Sambuc 
101*f4a2713aSLionel Sambuc // Non-type template parameters in scope
102*f4a2713aSLionel Sambuc template<int Size>
103*f4a2713aSLionel Sambuc void f(int& i) {
104*f4a2713aSLionel Sambuc   i = Size;
105*f4a2713aSLionel Sambuc   Size = i; // expected-error{{expression is not assignable}}
106*f4a2713aSLionel Sambuc }
107*f4a2713aSLionel Sambuc 
108*f4a2713aSLionel Sambuc template<typename T>
109*f4a2713aSLionel Sambuc const T& min(const T&, const T&);
110*f4a2713aSLionel Sambuc 
111*f4a2713aSLionel Sambuc void f2() {
112*f4a2713aSLionel Sambuc   int x;
113*f4a2713aSLionel Sambuc   A< typeof(x>1) > a;
114*f4a2713aSLionel Sambuc }
115*f4a2713aSLionel Sambuc 
116*f4a2713aSLionel Sambuc 
117*f4a2713aSLionel Sambuc // PR3844
118*f4a2713aSLionel Sambuc template <> struct S<int> { }; // expected-error{{explicit specialization of non-template struct 'S'}}
119*f4a2713aSLionel Sambuc template <> union U<int> { }; // expected-error{{explicit specialization of non-template union 'U'}}
120*f4a2713aSLionel Sambuc 
121*f4a2713aSLionel Sambuc namespace PR6184 {
122*f4a2713aSLionel Sambuc   namespace N {
123*f4a2713aSLionel Sambuc     template <typename T>
124*f4a2713aSLionel Sambuc     void bar(typename T::x);
125*f4a2713aSLionel Sambuc   }
126*f4a2713aSLionel Sambuc 
127*f4a2713aSLionel Sambuc   template <typename T>
128*f4a2713aSLionel Sambuc   void N::bar(typename T::x) { }
129*f4a2713aSLionel Sambuc }
130