Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -fsyntax-only -verify %s |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2 | template<typename T, typename U = int> struct A; // expected-note {{template is declared here}} \ |
| 3 | // expected-note{{explicitly specialized}} |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5 | template<> struct A<double, double>; // expected-note{{forward declaration}} |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7 | template<> struct A<float, float> { // expected-note{{previous definition}} |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 8 | int x; |
| 9 | }; |
| 10 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 11 | template<> struct A<float> { // expected-note{{previous definition}} |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 12 | int y; |
| 13 | }; |
| 14 | |
| 15 | int test_specs(A<float, float> *a1, A<float, int> *a2) { |
| 16 | return a1->x + a2->y; |
| 17 | } |
| 18 | |
| 19 | int test_incomplete_specs(A<double, double> *a1, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 20 | A<double> *a2) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 21 | { |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 22 | (void)a1->x; // expected-error{{member access into incomplete type}} |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 23 | (void)a2->x; // expected-error{{implicit instantiation of undefined template 'struct A<double, int>'}} |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | typedef float FLOAT; |
| 27 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 28 | template<> struct A<float, FLOAT>; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 29 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 30 | template<> struct A<FLOAT, float> { }; // expected-error{{redefinition}} |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 31 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 32 | template<> struct A<float, int> { }; // expected-error{{redefinition}} |
Douglas Gregor | 611a8c4 | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 34 | template<typename T, typename U = int> struct X; |
Douglas Gregor | 611a8c4 | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 35 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 36 | template <> struct X<int, int> { int foo(); }; // #1 |
| 37 | template <> struct X<float> { int bar(); }; // #2 |
Douglas Gregor | 611a8c4 | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 38 | |
| 39 | typedef int int_type; |
| 40 | void testme(X<int_type> *x1, X<float, int> *x2) { |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 41 | (void)x1->foo(); // okay: refers to #1 |
| 42 | (void)x2->bar(); // okay: refers to #2 |
Douglas Gregor | 611a8c4 | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 43 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 44 | |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 45 | // Make sure specializations are proper classes. |
| 46 | template<> |
| 47 | struct A<char> { |
| 48 | A(); |
| 49 | }; |
| 50 | |
| 51 | A<char>::A() { } |
| 52 | |
Douglas Gregor | db3a0f5 | 2009-08-26 18:54:58 +0000 | [diff] [blame] | 53 | // Make sure we can see specializations defined before the primary template. |
| 54 | namespace N{ |
| 55 | template<typename T> struct A0; |
| 56 | } |
| 57 | |
| 58 | namespace N { |
| 59 | template<> |
| 60 | struct A0<void> { |
| 61 | typedef void* pointer; |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | namespace N { |
| 66 | template<typename T> |
| 67 | struct A0 { |
| 68 | void foo(A0<void>::pointer p = 0); |
| 69 | }; |
| 70 | } |
| 71 | |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 72 | // Diagnose specialization errors |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 73 | struct A<double> { }; // expected-error{{template specialization requires 'template<>'}} |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 75 | template<> struct ::A<double>; |
| 76 | |
| 77 | namespace N { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 78 | template<typename T> struct B; // expected-note 2{{explicitly specialized}} |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 80 | template<> struct ::N::B<char>; // okay |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 81 | template<> struct ::N::B<short>; // okay |
| 82 | template<> struct ::N::B<int>; // okay |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 83 | |
| 84 | int f(int); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | template<> struct N::B<int> { }; // okay |
| 88 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 89 | template<> struct N::B<float> { }; // expected-error{{originally}} |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 90 | |
| 91 | namespace M { |
| 92 | template<> struct ::N::B<short> { }; // expected-error{{class template specialization of 'B' not in a namespace enclosing 'N'}} |
| 93 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 94 | template<> struct ::A<long double>; // expected-error{{originally}} |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 95 | } |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 96 | |
| 97 | template<> struct N::B<char> { |
| 98 | int testf(int x) { return f(x); } |
| 99 | }; |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 100 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 101 | // PR5264 |
| 102 | template <typename T> class Foo; |
| 103 | Foo<int>* v; |
| 104 | Foo<int>& F() { return *v; } |
| 105 | template <typename T> class Foo {}; |
| 106 | Foo<int> x; |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 107 | |
| 108 | |
| 109 | // Template template parameters |
| 110 | template<template<class T> class Wibble> |
| 111 | class Wibble<int> { }; // expected-error{{cannot specialize a template template parameter}} |