blob: 0292c1b8ff20f38732ed5d8579933a4e0c919f2b [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Douglas Gregor5c0405d2009-10-07 22:35:40 +00002template<typename T, typename U = int> struct A; // expected-note {{template is declared here}} \
3 // expected-note{{explicitly specialized}}
Douglas Gregor67a65642009-02-17 23:15:12 +00004
Douglas Gregorf47b9112009-02-25 22:02:03 +00005template<> struct A<double, double>; // expected-note{{forward declaration}}
Douglas Gregor67a65642009-02-17 23:15:12 +00006
Douglas Gregorf47b9112009-02-25 22:02:03 +00007template<> struct A<float, float> { // expected-note{{previous definition}}
Douglas Gregor67a65642009-02-17 23:15:12 +00008 int x;
9};
10
Douglas Gregorf47b9112009-02-25 22:02:03 +000011template<> struct A<float> { // expected-note{{previous definition}}
Douglas Gregor67a65642009-02-17 23:15:12 +000012 int y;
13};
14
15int test_specs(A<float, float> *a1, A<float, int> *a2) {
16 return a1->x + a2->y;
17}
18
19int test_incomplete_specs(A<double, double> *a1,
Douglas Gregor463421d2009-03-03 04:44:36 +000020 A<double> *a2)
Douglas Gregor67a65642009-02-17 23:15:12 +000021{
Douglas Gregor3fad6172009-11-17 05:17:33 +000022 (void)a1->x; // expected-error{{member access into incomplete type}}
John McCall85f90552010-03-10 11:27:22 +000023 (void)a2->x; // expected-error{{implicit instantiation of undefined template 'A<double, int>'}}
Douglas Gregor67a65642009-02-17 23:15:12 +000024}
25
26typedef float FLOAT;
27
Douglas Gregorf47b9112009-02-25 22:02:03 +000028template<> struct A<float, FLOAT>;
Douglas Gregor67a65642009-02-17 23:15:12 +000029
Douglas Gregorf47b9112009-02-25 22:02:03 +000030template<> struct A<FLOAT, float> { }; // expected-error{{redefinition}}
Douglas Gregor67a65642009-02-17 23:15:12 +000031
Douglas Gregorf47b9112009-02-25 22:02:03 +000032template<> struct A<float, int> { }; // expected-error{{redefinition}}
Douglas Gregor0f3dd9a2009-02-19 00:52:42 +000033
Douglas Gregorf47b9112009-02-25 22:02:03 +000034template<typename T, typename U = int> struct X;
Douglas Gregor0f3dd9a2009-02-19 00:52:42 +000035
Douglas Gregorf47b9112009-02-25 22:02:03 +000036template <> struct X<int, int> { int foo(); }; // #1
37template <> struct X<float> { int bar(); }; // #2
Douglas Gregor0f3dd9a2009-02-19 00:52:42 +000038
39typedef int int_type;
40void testme(X<int_type> *x1, X<float, int> *x2) {
Douglas Gregor8d092162009-02-26 00:02:51 +000041 (void)x1->foo(); // okay: refers to #1
42 (void)x2->bar(); // okay: refers to #2
Douglas Gregor0f3dd9a2009-02-19 00:52:42 +000043}
Douglas Gregor7f741122009-02-25 19:37:18 +000044
Douglas Gregor8d092162009-02-26 00:02:51 +000045// Make sure specializations are proper classes.
46template<>
47struct A<char> {
48 A();
49};
50
51A<char>::A() { }
52
Douglas Gregor13789b32009-08-26 18:54:58 +000053// Make sure we can see specializations defined before the primary template.
54namespace N{
55 template<typename T> struct A0;
56}
57
58namespace N {
59 template<>
60 struct A0<void> {
61 typedef void* pointer;
62 };
63}
64
65namespace N {
66 template<typename T>
67 struct A0 {
68 void foo(A0<void>::pointer p = 0);
69 };
70}
71
Douglas Gregor8d092162009-02-26 00:02:51 +000072// Diagnose specialization errors
Douglas Gregoref6ab412009-10-27 06:26:26 +000073struct A<double> { }; // expected-error{{template specialization requires 'template<>'}}
Douglas Gregorf47b9112009-02-25 22:02:03 +000074
Douglas Gregorf47b9112009-02-25 22:02:03 +000075template<> struct ::A<double>;
76
77namespace N {
Douglas Gregor5c0405d2009-10-07 22:35:40 +000078 template<typename T> struct B; // expected-note 2{{explicitly specialized}}
Douglas Gregorf47b9112009-02-25 22:02:03 +000079
Douglas Gregor1e249f82009-02-25 22:18:32 +000080 template<> struct ::N::B<char>; // okay
Douglas Gregorf47b9112009-02-25 22:02:03 +000081 template<> struct ::N::B<short>; // okay
82 template<> struct ::N::B<int>; // okay
Douglas Gregor1e249f82009-02-25 22:18:32 +000083
84 int f(int);
Douglas Gregorf47b9112009-02-25 22:02:03 +000085}
86
87template<> struct N::B<int> { }; // okay
88
Richard Smithe4345902011-12-29 21:57:33 +000089template<> struct N::B<float> { }; // expected-warning{{C++11 extension}}
Douglas Gregorf47b9112009-02-25 22:02:03 +000090
91namespace M {
92 template<> struct ::N::B<short> { }; // expected-error{{class template specialization of 'B' not in a namespace enclosing 'N'}}
93
Richard Smitha98f8fc2013-12-07 05:09:50 +000094 template<> struct ::A<long double>; // expected-error{{must occur at global scope}}
Douglas Gregorf47b9112009-02-25 22:02:03 +000095}
Douglas Gregor1e249f82009-02-25 22:18:32 +000096
97template<> struct N::B<char> {
98 int testf(int x) { return f(x); }
99};
Douglas Gregor8d092162009-02-26 00:02:51 +0000100
Douglas Gregoref6ab412009-10-27 06:26:26 +0000101// PR5264
102template <typename T> class Foo;
103Foo<int>* v;
104Foo<int>& F() { return *v; }
105template <typename T> class Foo {};
106Foo<int> x;
Douglas Gregordd6c0352009-11-12 00:46:20 +0000107
108
109// Template template parameters
110template<template<class T> class Wibble>
111class Wibble<int> { }; // expected-error{{cannot specialize a template template parameter}}
Douglas Gregor44e5a0a2011-10-20 16:41:18 +0000112
113namespace 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 Smith6056d5e2014-02-09 00:54:43 +0000122
123namespace 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
144namespace 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
167namespace 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}