class-template-spec.cpp revision 98137534e612c274ba270af99d73429043957e53
1// RUN: clang -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// Diagnose specialization errors
53struct A<double> { }; // expected-error{{template specialization requires 'template<>'}}
54
55template<typename T> // expected-error{{class template partial specialization is not yet supported}}
56struct A<T*> { };
57
58template<> struct ::A<double>;
59
60namespace N {
61  template<typename T> struct B; // expected-note 2{{template is declared here}}
62
63  template<> struct ::N::B<char>; // okay
64  template<> struct ::N::B<short>; // okay
65  template<> struct ::N::B<int>; // okay
66
67  int f(int);
68}
69
70template<> struct N::B<int> { }; // okay
71
72template<> struct N::B<float> { }; // expected-error{{class template specialization of 'B' not in namespace 'N'}}
73
74namespace M {
75  template<> struct ::N::B<short> { }; // expected-error{{class template specialization of 'B' not in a namespace enclosing 'N'}}
76
77  template<> struct ::A<long double>; // expected-error{{class template specialization of 'A' must occur in the global scope}}
78}
79
80template<> struct N::B<char> {
81  int testf(int x) { return f(x); }
82};
83
84