blob: 7e931a31fa2d5170a8f7a086e3709f2cd8db1243 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Douglas Gregoradcac882008-12-01 23:54:00 +00002
3// Errors
4export class foo { }; // expected-error {{expected template}}
Douglas Gregord5a423b2009-09-25 18:43:00 +00005template x; // expected-error {{C++ requires a type specifier for all declarations}} \
6 // expected-error {{does not refer}}
Douglas Gregor7cdbc582009-07-22 23:48:44 +00007export template x; // expected-error {{expected '<' after 'template'}}
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00008export template<class T> class x0; // expected-warning {{exported templates are unsupported}}
David Blaikieb031eab2012-04-06 23:33:59 +00009template < ; // expected-error {{expected template parameter}} \
Douglas Gregor99ea7342010-10-15 01:15:58 +000010// expected-error{{expected ',' or '>' in template-parameter-list}} \
11// expected-warning {{declaration does not declare anything}}
David Blaikieeb52f86a2012-04-09 16:37:11 +000012template <int +> struct x1; // expected-error {{expected ',' or '>' in template-parameter-list}}
13
14// verifies that we only walk to the ',' & still produce errors on the rest of the template parameters
15template <int +, T> struct x2; // expected-error {{expected ',' or '>' in template-parameter-list}} \
16 expected-error {{expected unqualified-id}}
17template<template<int+>> struct x3; // expected-error {{expected ',' or '>' in template-parameter-list}} \
18 expected-error {{template template parameter requires 'class' after the parameter list}}
Douglas Gregorf6b11852009-10-08 15:14:33 +000019template <template X> struct Err1; // expected-error {{expected '<' after 'template'}} \
20// expected-error{{extraneous}}
David Blaikie673720d2012-04-06 06:28:32 +000021template <template <typename> > struct Err2; // expected-error {{template template parameter requires 'class' after the parameter list}}
22template <template <typename> Foo> struct Err3; // expected-error {{template template parameter requires 'class' after the parameter list}}
Douglas Gregoradcac882008-12-01 23:54:00 +000023
24// Template function declarations
25template <typename T> void foo();
26template <typename T, typename U> void foo();
27
Douglas Gregor26236e82008-12-02 00:41:28 +000028// Template function definitions.
29template <typename T> void foo() { }
Douglas Gregoradcac882008-12-01 23:54:00 +000030
31// Template class (forward) declarations
32template <typename T> struct A;
33template <typename T, typename U> struct b;
34template <typename> struct C;
35template <typename, typename> struct D;
36
37// Forward declarations with default parameters?
Douglas Gregor4310f4e2009-02-16 22:38:20 +000038template <typename T = int> class X1;
39template <typename = int> class X2;
Douglas Gregoradcac882008-12-01 23:54:00 +000040
41// Forward declarations w/template template parameters
42template <template <typename> class T> class TTP1;
43template <template <typename> class> class TTP2;
Douglas Gregore53060f2009-06-25 22:08:12 +000044template <template <typename> class T = foo> class TTP3; // expected-error{{must be a class template}}
45template <template <typename> class = foo> class TTP3; // expected-error{{must be a class template}}
Douglas Gregoraaba5e32009-02-04 19:02:06 +000046template <template <typename X, typename Y> class T> class TTP5;
Douglas Gregoradcac882008-12-01 23:54:00 +000047
Douglas Gregor52591bf2009-06-24 00:54:41 +000048// Forward declarations with non-type params
Douglas Gregoradcac882008-12-01 23:54:00 +000049template <int> class NTP0;
50template <int N> class NTP1;
51template <int N = 5> class NTP2;
52template <int = 10> class NTP3;
Douglas Gregor4310f4e2009-02-16 22:38:20 +000053template <unsigned int N = 12u> class NTP4;
54template <unsigned int = 12u> class NTP5;
55template <unsigned = 15u> class NTP6;
56template <typename T, T Obj> class NTP7;
Douglas Gregoradcac882008-12-01 23:54:00 +000057
58// Template class declarations
59template <typename T> struct A { };
60template <typename T, typename U> struct B { };
61
Douglas Gregor72c3f312008-12-05 18:15:24 +000062// Template parameter shadowing
63template<typename T, // expected-note{{template parameter is declared here}}
Mike Stump1eb44332009-09-09 15:08:12 +000064 typename T> // expected-error{{declaration of 'T' shadows template parameter}}
Douglas Gregor72c3f312008-12-05 18:15:24 +000065 void shadow1();
66
67template<typename T> // expected-note{{template parameter is declared here}}
68void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}}
69
70template<typename T> // expected-note{{template parameter is declared here}}
71class T { // expected-error{{declaration of 'T' shadows template parameter}}
72};
73
74template<int Size> // expected-note{{template parameter is declared here}}
75void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}}
76
Douglas Gregorc19ee3e2009-06-17 23:37:01 +000077// <rdar://problem/6952203>
78template<typename T> // expected-note{{here}}
79struct shadow4 {
80 int T; // expected-error{{shadows}}
81};
82
83template<typename T> // expected-note{{here}}
84struct shadow5 {
85 int T(int, float); // expected-error{{shadows}}
86};
87
Douglas Gregor72c3f312008-12-05 18:15:24 +000088// Non-type template parameters in scope
89template<int Size>
90void f(int& i) {
91 i = Size;
92 Size = i; // expected-error{{expression is not assignable}}
93}
94
95template<typename T>
96const T& min(const T&, const T&);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +000097
98void f2() {
99 int x;
100 A< typeof(x>1) > a;
101}
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000102
103
104// PR3844
105template <> struct S<int> { }; // expected-error{{explicit specialization of non-template struct 'S'}}
Douglas Gregor1df0ee92010-02-05 07:07:10 +0000106
107namespace PR6184 {
108 namespace N {
109 template <typename T>
110 void bar(typename T::x);
111 }
112
113 template <typename T>
114 void N::bar(typename T::x) { }
115}