Daniel Dunbar | a45cf5b | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -fsyntax-only -verify %s |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2 | template<typename T, typename U = int> struct A; // expected-note 2{{template is declared here}} |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4 | template<> struct A<double, double>; // expected-note{{forward declaration}} |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6 | template<> struct A<float, float> { // expected-note{{previous definition}} |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 7 | int x; |
| 8 | }; |
| 9 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 10 | template<> struct A<float> { // expected-note{{previous definition}} |
Douglas Gregor | 67a6564 | 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, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 19 | A<double> *a2) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 20 | { |
| 21 | (void)a1->x; // expected-error{{incomplete definition of type 'A<double, double>'}} |
Douglas Gregor | 65b2c4c | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 22 | (void)a2->x; // expected-error{{implicit instantiation of undefined template 'struct A<double, int>'}} |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | typedef float FLOAT; |
| 26 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 27 | template<> struct A<float, FLOAT>; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 29 | template<> struct A<FLOAT, float> { }; // expected-error{{redefinition}} |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 31 | template<> struct A<float, int> { }; // expected-error{{redefinition}} |
Douglas Gregor | 0f3dd9a | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 32 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 33 | template<typename T, typename U = int> struct X; |
Douglas Gregor | 0f3dd9a | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 34 | |
Douglas Gregor | f47b911 | 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 | 0f3dd9a | 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 | 8d09216 | 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 | 0f3dd9a | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 42 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 43 | |
Douglas Gregor | 8d09216 | 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 | |
Douglas Gregor | 13789b3 | 2009-08-26 18:54:58 +0000 | [diff] [blame^] | 52 | // Make sure we can see specializations defined before the primary template. |
| 53 | namespace N{ |
| 54 | template<typename T> struct A0; |
| 55 | } |
| 56 | |
| 57 | namespace N { |
| 58 | template<> |
| 59 | struct A0<void> { |
| 60 | typedef void* pointer; |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | namespace N { |
| 65 | template<typename T> |
| 66 | struct A0 { |
| 67 | void foo(A0<void>::pointer p = 0); |
| 68 | }; |
| 69 | } |
| 70 | |
Douglas Gregor | 8d09216 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 71 | // Diagnose specialization errors |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 72 | struct A<double> { }; // expected-error{{template specialization requires 'template<>'}} |
| 73 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 74 | template<> struct ::A<double>; |
| 75 | |
| 76 | namespace N { |
| 77 | template<typename T> struct B; // expected-note 2{{template is declared here}} |
| 78 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 79 | template<> struct ::N::B<char>; // okay |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 80 | template<> struct ::N::B<short>; // okay |
| 81 | template<> struct ::N::B<int>; // okay |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 82 | |
| 83 | int f(int); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | template<> struct N::B<int> { }; // okay |
| 87 | |
| 88 | template<> struct N::B<float> { }; // expected-error{{class template specialization of 'B' not in namespace 'N'}} |
| 89 | |
| 90 | namespace 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 Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 95 | |
| 96 | template<> struct N::B<char> { |
| 97 | int testf(int x) { return f(x); } |
| 98 | }; |
Douglas Gregor | 8d09216 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 99 | |