Daniel Dunbar | 8fbe78f | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Douglas Gregor | 5c0405d | 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 | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5 | template<> struct A<double, double>; // expected-note{{forward declaration}} |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 7 | template<> struct A<float, float> { // expected-note{{previous definition}} |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 8 | int x; |
| 9 | }; |
| 10 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 11 | template<> struct A<float> { // expected-note{{previous definition}} |
Douglas Gregor | 67a6564 | 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 | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 20 | A<double> *a2) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 21 | { |
Douglas Gregor | 3fad617 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 22 | (void)a1->x; // expected-error{{member access into incomplete type}} |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 23 | (void)a2->x; // expected-error{{implicit instantiation of undefined template 'A<double, int>'}} |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | typedef float FLOAT; |
| 27 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 28 | template<> struct A<float, FLOAT>; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 29 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 30 | template<> struct A<FLOAT, float> { }; // expected-error{{redefinition}} |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 31 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 32 | template<> struct A<float, int> { }; // expected-error{{redefinition}} |
Douglas Gregor | 0f3dd9a | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 34 | template<typename T, typename U = int> struct X; |
Douglas Gregor | 0f3dd9a | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 35 | |
Douglas Gregor | f47b911 | 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 | 0f3dd9a | 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 | 8d09216 | 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 | 0f3dd9a | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 43 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 44 | |
Douglas Gregor | 8d09216 | 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 | 13789b3 | 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 | 8d09216 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 72 | // Diagnose specialization errors |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 73 | struct A<double> { }; // expected-error{{template specialization requires 'template<>'}} |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 75 | template<> struct ::A<double>; |
| 76 | |
| 77 | namespace N { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 78 | template<typename T> struct B; // expected-note 2{{explicitly specialized}} |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 80 | template<> struct ::N::B<char>; // okay |
Douglas Gregor | f47b911 | 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 | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 83 | |
| 84 | int f(int); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | template<> struct N::B<int> { }; // okay |
| 88 | |
Richard Smith | e434590 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 89 | template<> struct N::B<float> { }; // expected-warning{{C++11 extension}} |
Douglas Gregor | f47b911 | 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 | |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 94 | template<> struct ::A<long double>; // expected-error{{must occur at global scope}} |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 95 | } |
Douglas Gregor | 1e249f8 | 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 | 8d09216 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 100 | |
Douglas Gregor | ef6ab41 | 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 | dd6c035 | 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}} |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 112 | |
| 113 | namespace rdar9676205 { |
| 114 | template<typename T> |
| 115 | struct X { |
| 116 | template<typename U> |
| 117 | struct X<U*> { // expected-error{{explicit specialization of 'X' in class scope}} |
| 118 | }; |
| 119 | }; |
| 120 | |
| 121 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 122 | |
| 123 | namespace PR18009 { |
| 124 | template <typename T> struct A { |
| 125 | template <int N, int M> struct S; |
| 126 | template <int N> struct S<N, sizeof(T)> {}; |
| 127 | }; |
| 128 | A<int>::S<8, sizeof(int)> a; // ok |
| 129 | |
| 130 | template <typename T> struct B { |
| 131 | template <int N, int M> struct S; // expected-note {{declared here}} |
| 132 | template <int N> struct S<N, sizeof(T) + |
| 133 | N // expected-error {{non-type template argument depends on a template parameter of the partial specialization}} |
| 134 | > {}; |
| 135 | }; |
| 136 | B<int>::S<8, sizeof(int) + 8> s; // expected-error {{undefined}} |
| 137 | |
| 138 | template<int A> struct outer { |
| 139 | template<int B, int C> struct inner {}; |
| 140 | template<int C> struct inner<A * 2, C> {}; |
| 141 | }; |
| 142 | } |
| 143 | |
| 144 | namespace PR16519 { |
| 145 | template<typename T, T...N> struct integer_sequence { typedef T value_type; }; // expected-warning {{extension}} |
| 146 | |
| 147 | template<typename T> struct __make_integer_sequence; |
| 148 | template<typename T, T N> using make_integer_sequence = typename __make_integer_sequence<T>::template make<N, N % 2>::type; // expected-warning {{extension}} |
| 149 | |
| 150 | template<typename T, typename T::value_type ...Extra> struct __make_integer_sequence_impl; // expected-warning {{extension}} |
| 151 | template<typename T, T ...N, T ...Extra> struct __make_integer_sequence_impl<integer_sequence<T, N...>, Extra...> { // expected-warning 2{{extension}} |
| 152 | typedef integer_sequence<T, N..., sizeof...(N) + N..., Extra...> type; |
| 153 | }; |
| 154 | |
| 155 | template<typename T> struct __make_integer_sequence { |
| 156 | template<T N, T Parity, typename = void> struct make; |
| 157 | template<typename Dummy> struct make<0, 0, Dummy> { typedef integer_sequence<T> type; }; |
| 158 | template<typename Dummy> struct make<1, 1, Dummy> { typedef integer_sequence<T, 0> type; }; |
| 159 | template<T N, typename Dummy> struct make<N, 0, Dummy> : __make_integer_sequence_impl<make_integer_sequence<T, N/2> > {}; |
| 160 | template<T N, typename Dummy> struct make<N, 1, Dummy> : __make_integer_sequence_impl<make_integer_sequence<T, N/2>, N - 1> {}; |
| 161 | }; |
| 162 | |
| 163 | using X = make_integer_sequence<int, 5>; // expected-warning {{extension}} |
| 164 | using X = integer_sequence<int, 0, 1, 2, 3, 4>; // expected-warning {{extension}} |
| 165 | } |
| 166 | |
| 167 | namespace DefaultArgVsPartialSpec { |
| 168 | // Check that the diagnostic points at the partial specialization, not just at |
| 169 | // the default argument. |
| 170 | template<typename T, int N = |
| 171 | sizeof(T) // expected-note {{template parameter is used in default argument declared here}} |
| 172 | > struct X {}; |
| 173 | template<typename T> struct X<T> {}; // expected-error {{non-type template argument depends on a template parameter of the partial specialization}} |
| 174 | |
| 175 | template<typename T, |
| 176 | T N = 0 // expected-note {{template parameter is declared here}} |
| 177 | > struct S; |
| 178 | template<typename T> struct S<T> {}; // expected-error {{non-type template argument specializes a template parameter with dependent type 'T'}} |
| 179 | } |