class-template-spec.cpp revision db3a0f543e9a120d37823c6f2a2f1c693b69f2a1
1// RUN: clang-cc -fsyntax-only -verify %s
2template<typename T, typename U = int> struct A; // expected-note 2{{template is declared here}}
3
4template<> struct A<double, double>; // expected-note{{forward declaration}}
5
6template<> struct A<float, float> {  // expected-note{{previous definition}}
7  int x;
8};
9
10template<> struct A<float> { // expected-note{{previous definition}}
11  int y;
12};
13
14int test_specs(A<float, float> *a1, A<float, int> *a2) {
15  return a1->x + a2->y;
16}
17
18int test_incomplete_specs(A<double, double> *a1,
19                          A<double> *a2)
20{
21  (void)a1->x; // expected-error{{incomplete definition of type 'A<double, double>'}}
22  (void)a2->x; // expected-error{{implicit instantiation of undefined template 'struct A<double, int>'}}
23}
24
25typedef float FLOAT;
26
27template<> struct A<float, FLOAT>;
28
29template<> struct A<FLOAT, float> { }; // expected-error{{redefinition}}
30
31template<> struct A<float, int> { }; // expected-error{{redefinition}}
32
33template<typename T, typename U = int> struct X;
34
35template <> struct X<int, int> { int foo(); }; // #1
36template <> struct X<float> { int bar(); };  // #2
37
38typedef int int_type;
39void testme(X<int_type> *x1, X<float, int> *x2) {
40  (void)x1->foo(); // okay: refers to #1
41  (void)x2->bar(); // okay: refers to #2
42}
43
44// Make sure specializations are proper classes.
45template<>
46struct A<char> {
47  A();
48};
49
50A<char>::A() { }
51
52// Make sure we can see specializations defined before the primary template.
53namespace N{
54  template<typename T> struct A0;
55}
56
57namespace N {
58  template<>
59  struct A0<void> {
60    typedef void* pointer;
61  };
62}
63
64namespace N {
65  template<typename T>
66  struct A0 {
67    void foo(A0<void>::pointer p = 0);
68  };
69}
70
71// Diagnose specialization errors
72struct A<double> { }; // expected-error{{template specialization requires 'template<>'}}
73
74template<> struct ::A<double>;
75
76namespace N {
77  template<typename T> struct B; // expected-note 2{{template is declared here}}
78
79  template<> struct ::N::B<char>; // okay
80  template<> struct ::N::B<short>; // okay
81  template<> struct ::N::B<int>; // okay
82
83  int f(int);
84}
85
86template<> struct N::B<int> { }; // okay
87
88template<> struct N::B<float> { }; // expected-error{{class template specialization of 'B' not in namespace 'N'}}
89
90namespace M {
91  template<> struct ::N::B<short> { }; // expected-error{{class template specialization of 'B' not in a namespace enclosing 'N'}}
92
93  template<> struct ::A<long double>; // expected-error{{class template specialization of 'A' must occur in the global scope}}
94}
95
96template<> struct N::B<char> {
97  int testf(int x) { return f(x); }
98};
99
100