cxx-template-decl.cpp revision d684b0027e16163c4bdba3e2f8bfadda7d62a0d3
1// RUN: clang -fsyntax-only -verify %s
2
3// Errors
4export class foo { };   // expected-error {{expected template}}
5template  x;            // expected-error {{expected '<' after 'template'}}
6export template x;      // expected-error {{expected '<' after 'template'}} \
7                        // expected-note {{exported templates are unsupported}}
8// See Sema::ParsedFreeStandingDeclSpec about the double diagnostic. This is
9// because ParseNonTypeTemplateParameter starts parsing a DeclSpec.
10template < ;            // expected-error {{parse error}} expected-error {{declaration does not declare anything}}
11template <template X> struct Err1; // expected-error {{expected '<' after 'template'}}
12template <template <typename> > struct Err2;       // expected-error {{expected 'class' before '>'}}
13template <template <typename> Foo> struct Err3;    // expected-error {{expected 'class' before 'Foo'}}
14
15// Template function declarations
16template <typename T> void foo();
17template <typename T, typename U> void foo();
18
19// Template function definitions.
20template <typename T> void foo() { }
21
22// Template class (forward) declarations
23template <typename T> struct A;
24template <typename T, typename U> struct b;
25template <typename> struct C;
26template <typename, typename> struct D;
27
28// Forward declarations with default parameters?
29template <typename T = int> X1;
30template <typename = int> X2;
31
32// Forward declarations w/template template parameters
33template <template <typename> class T> class TTP1;
34template <template <typename> class> class TTP2;
35template <template <typename> class T = foo> class TTP3; // FIXME:expected-error{{template argument for template template parameter must be a template}}
36template <template <typename> class = foo> class TTP3; // FIXME:expected-error{{template argument for template template parameter must be a template}}
37template <template <typename X, typename Y> class T> class TTP5;
38
39// Forward declararations with non-type params
40template <int> class NTP0;
41template <int N> class NTP1;
42template <int N = 5> class NTP2;
43template <int = 10> class NTP3;
44template <unsigned int N = 12u> NTP4;;
45template <unsigned int = 12u> NTP5;
46template <unsigned = 15u> NTP6;
47template <typename T, T Obj> NTP7;
48
49// Template class declarations
50template <typename T> struct A { };
51template <typename T, typename U> struct B { };
52
53// Template parameter shadowing
54template<typename T, // expected-note{{template parameter is declared here}}
55	 typename T> // expected-error{{declaration of 'T' shadows template parameter}}
56  void shadow1();
57
58template<typename T> // expected-note{{template parameter is declared here}}
59void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}}
60
61template<typename T> // expected-note{{template parameter is declared here}}
62class T { // expected-error{{declaration of 'T' shadows template parameter}}
63};
64
65template<int Size> // expected-note{{template parameter is declared here}}
66void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}}
67
68// Non-type template parameters in scope
69template<int Size>
70void f(int& i) {
71  i = Size;
72  Size = i; // expected-error{{expression is not assignable}}
73}
74
75template<typename T>
76const T& min(const T&, const T&);
77