blob: 34c616cc2f42dd42edef3454b983f0945c17a358 [file] [log] [blame]
Daniel Dunbara45cf5b2009-03-24 02:24:46 +00001// RUN: clang-cc -fsyntax-only -verify %s
Douglas Gregor463421d2009-03-03 04:44:36 +00002template<typename T, typename U = int> struct A; // expected-note 2{{template is declared here}}
Douglas Gregor67a65642009-02-17 23:15:12 +00003
Douglas Gregorf47b9112009-02-25 22:02:03 +00004template<> struct A<double, double>; // expected-note{{forward declaration}}
Douglas Gregor67a65642009-02-17 23:15:12 +00005
Douglas Gregorf47b9112009-02-25 22:02:03 +00006template<> struct A<float, float> { // expected-note{{previous definition}}
Douglas Gregor67a65642009-02-17 23:15:12 +00007 int x;
8};
9
Douglas Gregorf47b9112009-02-25 22:02:03 +000010template<> struct A<float> { // expected-note{{previous definition}}
Douglas Gregor67a65642009-02-17 23:15:12 +000011 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,
Douglas Gregor463421d2009-03-03 04:44:36 +000019 A<double> *a2)
Douglas Gregor67a65642009-02-17 23:15:12 +000020{
21 (void)a1->x; // expected-error{{incomplete definition of type 'A<double, double>'}}
Douglas Gregor65b2c4c2009-03-10 18:33:27 +000022 (void)a2->x; // expected-error{{implicit instantiation of undefined template 'struct A<double, int>'}}
Douglas Gregor67a65642009-02-17 23:15:12 +000023}
24
25typedef float FLOAT;
26
Douglas Gregorf47b9112009-02-25 22:02:03 +000027template<> struct A<float, FLOAT>;
Douglas Gregor67a65642009-02-17 23:15:12 +000028
Douglas Gregorf47b9112009-02-25 22:02:03 +000029template<> struct A<FLOAT, float> { }; // expected-error{{redefinition}}
Douglas Gregor67a65642009-02-17 23:15:12 +000030
Douglas Gregorf47b9112009-02-25 22:02:03 +000031template<> struct A<float, int> { }; // expected-error{{redefinition}}
Douglas Gregor0f3dd9a2009-02-19 00:52:42 +000032
Douglas Gregorf47b9112009-02-25 22:02:03 +000033template<typename T, typename U = int> struct X;
Douglas Gregor0f3dd9a2009-02-19 00:52:42 +000034
Douglas Gregorf47b9112009-02-25 22:02:03 +000035template <> struct X<int, int> { int foo(); }; // #1
36template <> struct X<float> { int bar(); }; // #2
Douglas Gregor0f3dd9a2009-02-19 00:52:42 +000037
38typedef int int_type;
39void testme(X<int_type> *x1, X<float, int> *x2) {
Douglas Gregor8d092162009-02-26 00:02:51 +000040 (void)x1->foo(); // okay: refers to #1
41 (void)x2->bar(); // okay: refers to #2
Douglas Gregor0f3dd9a2009-02-19 00:52:42 +000042}
Douglas Gregor7f741122009-02-25 19:37:18 +000043
Douglas Gregor8d092162009-02-26 00:02:51 +000044// Make sure specializations are proper classes.
45template<>
46struct A<char> {
47 A();
48};
49
50A<char>::A() { }
51
Douglas Gregor13789b32009-08-26 18:54:58 +000052// 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
Douglas Gregor8d092162009-02-26 00:02:51 +000071// Diagnose specialization errors
Douglas Gregorf47b9112009-02-25 22:02:03 +000072struct A<double> { }; // expected-error{{template specialization requires 'template<>'}}
73
Douglas Gregorf47b9112009-02-25 22:02:03 +000074template<> struct ::A<double>;
75
76namespace N {
77 template<typename T> struct B; // expected-note 2{{template is declared here}}
78
Douglas Gregor1e249f82009-02-25 22:18:32 +000079 template<> struct ::N::B<char>; // okay
Douglas Gregorf47b9112009-02-25 22:02:03 +000080 template<> struct ::N::B<short>; // okay
81 template<> struct ::N::B<int>; // okay
Douglas Gregor1e249f82009-02-25 22:18:32 +000082
83 int f(int);
Douglas Gregorf47b9112009-02-25 22:02:03 +000084}
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}
Douglas Gregor1e249f82009-02-25 22:18:32 +000095
96template<> struct N::B<char> {
97 int testf(int x) { return f(x); }
98};
Douglas Gregor8d092162009-02-26 00:02:51 +000099