Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1 | // RUN: clang -fsyntax-only -verify %s |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2 | template<typename T, typename U = int> struct A; // expected-note{{template is declared here}} |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4 | template<> struct A<double, double>; // expected-note{{forward declaration}} |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6 | template<> struct A<float, float> { // expected-note{{previous definition}} |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7 | int x; |
| 8 | }; |
| 9 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 10 | template<> struct A<float> { // expected-note{{previous definition}} |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 11 | int y; |
| 12 | }; |
| 13 | |
| 14 | int test_specs(A<float, float> *a1, A<float, int> *a2) { |
| 15 | return a1->x + a2->y; |
| 16 | } |
| 17 | |
| 18 | int test_incomplete_specs(A<double, double> *a1, |
| 19 | A<double> *a2) // FIXME: expected-note{{forward declaration}} |
| 20 | { |
| 21 | (void)a1->x; // expected-error{{incomplete definition of type 'A<double, double>'}} |
| 22 | (void)a2->x; // expected-error{{incomplete definition of type 'A<double>'}} |
| 23 | } |
| 24 | |
| 25 | typedef float FLOAT; |
| 26 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 27 | template<> struct A<float, FLOAT>; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 29 | template<> struct A<FLOAT, float> { }; // expected-error{{redefinition}} |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 31 | template<> struct A<float, int> { }; // expected-error{{redefinition}} |
Douglas Gregor | 611a8c4 | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 32 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 33 | template<typename T, typename U = int> struct X; |
Douglas Gregor | 611a8c4 | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 34 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 35 | template <> struct X<int, int> { int foo(); }; // #1 |
| 36 | template <> struct X<float> { int bar(); }; // #2 |
Douglas Gregor | 611a8c4 | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 37 | |
| 38 | typedef int int_type; |
| 39 | void testme(X<int_type> *x1, X<float, int> *x2) { |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame^] | 40 | (void)x1->foo(); // okay: refers to #1 |
| 41 | (void)x2->bar(); // okay: refers to #2 |
Douglas Gregor | 611a8c4 | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 42 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 43 | |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame^] | 44 | // Make sure specializations are proper classes. |
| 45 | template<> |
| 46 | struct A<char> { |
| 47 | A(); |
| 48 | }; |
| 49 | |
| 50 | A<char>::A() { } |
| 51 | |
| 52 | // Diagnose specialization errors |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 53 | struct A<double> { }; // expected-error{{template specialization requires 'template<>'}} |
| 54 | |
| 55 | template<typename T> // expected-error{{class template partial specialization is not yet supported}} |
| 56 | struct A<T*> { }; |
| 57 | |
| 58 | template<> struct ::A<double>; |
| 59 | |
| 60 | namespace N { |
| 61 | template<typename T> struct B; // expected-note 2{{template is declared here}} |
| 62 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 63 | template<> struct ::N::B<char>; // okay |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 64 | template<> struct ::N::B<short>; // okay |
| 65 | template<> struct ::N::B<int>; // okay |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 66 | |
| 67 | int f(int); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | template<> struct N::B<int> { }; // okay |
| 71 | |
| 72 | template<> struct N::B<float> { }; // expected-error{{class template specialization of 'B' not in namespace 'N'}} |
| 73 | |
| 74 | namespace 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 | } |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 79 | |
| 80 | template<> struct N::B<char> { |
| 81 | int testf(int x) { return f(x); } |
| 82 | }; |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame^] | 83 | |