xref: /minix3/external/bsd/llvm/dist/clang/test/PCH/cxx-templates.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // Header for PCH test cxx-templates.cpp
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc template <typename T1, typename T2>
4f4a2713aSLionel Sambuc struct S;
5f4a2713aSLionel Sambuc 
6f4a2713aSLionel Sambuc template <typename T1, typename T2>
7f4a2713aSLionel Sambuc struct S {
SS8f4a2713aSLionel Sambuc   S() { }
9f4a2713aSLionel Sambuc   static void templ();
10f4a2713aSLionel Sambuc };
11f4a2713aSLionel Sambuc 
12f4a2713aSLionel Sambuc template <typename T>
13f4a2713aSLionel Sambuc struct S<int, T> {
14f4a2713aSLionel Sambuc     static void partial();
15f4a2713aSLionel Sambuc };
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc template <>
18f4a2713aSLionel Sambuc struct S<int, float> {
19f4a2713aSLionel Sambuc     static void explicit_special();
20f4a2713aSLionel Sambuc };
21f4a2713aSLionel Sambuc 
22f4a2713aSLionel Sambuc template <int x>
23f4a2713aSLionel Sambuc int tmpl_f2() { return x; }
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc template <typename T, int y>
26f4a2713aSLionel Sambuc T templ_f(T x) {
27f4a2713aSLionel Sambuc   int z = templ_f<int, 5>(3);
28f4a2713aSLionel Sambuc   z = tmpl_f2<y+2>();
29f4a2713aSLionel Sambuc   T data[y];
30f4a2713aSLionel Sambuc   return x+y;
31f4a2713aSLionel Sambuc }
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc void govl(int);
34f4a2713aSLionel Sambuc void govl(char);
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc template <typename T>
37f4a2713aSLionel Sambuc struct Unresolv {
38f4a2713aSLionel Sambuc   void f() {
39f4a2713aSLionel Sambuc     govl(T());
40f4a2713aSLionel Sambuc   }
41f4a2713aSLionel Sambuc };
42f4a2713aSLionel Sambuc 
43f4a2713aSLionel Sambuc template <typename T>
44f4a2713aSLionel Sambuc struct Dep {
45f4a2713aSLionel Sambuc   typedef typename T::type Ty;
46f4a2713aSLionel Sambuc   void f() {
47f4a2713aSLionel Sambuc     Ty x = Ty();
48f4a2713aSLionel Sambuc     T::my_f();
49f4a2713aSLionel Sambuc     int y = T::template my_templf<int>(0);
50f4a2713aSLionel Sambuc     ovl(y);
51f4a2713aSLionel Sambuc   }
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc   void ovl(int);
54f4a2713aSLionel Sambuc   void ovl(float);
55f4a2713aSLionel Sambuc };
56f4a2713aSLionel Sambuc 
57f4a2713aSLionel Sambuc template<typename T, typename A1>
58f4a2713aSLionel Sambuc inline T make_a(const A1& a1) {
59f4a2713aSLionel Sambuc   T::depend_declref();
60f4a2713aSLionel Sambuc   return T(a1);
61f4a2713aSLionel Sambuc }
62f4a2713aSLionel Sambuc 
63f4a2713aSLionel Sambuc template <class T> class UseBase {
64f4a2713aSLionel Sambuc   void foo();
65f4a2713aSLionel Sambuc   typedef int bar;
66f4a2713aSLionel Sambuc };
67f4a2713aSLionel Sambuc 
68f4a2713aSLionel Sambuc template <class T> class UseA : public UseBase<T> {
69f4a2713aSLionel Sambuc   using UseBase<T>::foo;
70f4a2713aSLionel Sambuc   using typename UseBase<T>::bar;
71f4a2713aSLionel Sambuc };
72f4a2713aSLionel Sambuc 
73f4a2713aSLionel Sambuc template <class T> class Sub : public UseBase<int> { };
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc template <class _Ret, class _Tp>
76f4a2713aSLionel Sambuc   class mem_fun_t
77f4a2713aSLionel Sambuc   {
78f4a2713aSLionel Sambuc   public:
79f4a2713aSLionel Sambuc     explicit
80f4a2713aSLionel Sambuc     mem_fun_t(_Ret (_Tp::*__pf)())
81f4a2713aSLionel Sambuc      {}
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc   private:
84f4a2713aSLionel Sambuc     _Ret (_Tp::*_M_f)();
85f4a2713aSLionel Sambuc   };
86f4a2713aSLionel Sambuc 
87f4a2713aSLionel Sambuc template<unsigned N>
88f4a2713aSLionel Sambuc bool isInt(int x);
89f4a2713aSLionel Sambuc 
90f4a2713aSLionel Sambuc template<> bool isInt<8>(int x) {
91f4a2713aSLionel Sambuc   try { ++x; } catch(...) { --x; }
92f4a2713aSLionel Sambuc   return true;
93f4a2713aSLionel Sambuc }
94f4a2713aSLionel Sambuc 
95f4a2713aSLionel Sambuc template<typename _CharT>
96f4a2713aSLionel Sambuc int __copy_streambufs_eof(_CharT);
97f4a2713aSLionel Sambuc 
98f4a2713aSLionel Sambuc class basic_streambuf
99f4a2713aSLionel Sambuc {
100f4a2713aSLionel Sambuc   void m() { }
101f4a2713aSLionel Sambuc   friend int __copy_streambufs_eof<>(int);
102f4a2713aSLionel Sambuc };
103f4a2713aSLionel Sambuc 
104f4a2713aSLionel Sambuc // PR 7660
105f4a2713aSLionel Sambuc template<typename T> struct S_PR7660 { void g(void (*)(T)); };
106f4a2713aSLionel Sambuc  template<> void S_PR7660<int>::g(void(*)(int)) {}
107f4a2713aSLionel Sambuc 
108f4a2713aSLionel Sambuc // PR 7670
109f4a2713aSLionel Sambuc template<typename> class C_PR7670;
110f4a2713aSLionel Sambuc template<> class C_PR7670<int>;
111f4a2713aSLionel Sambuc template<> class C_PR7670<int>;
112f4a2713aSLionel Sambuc 
113f4a2713aSLionel Sambuc template <bool B>
114f4a2713aSLionel Sambuc struct S2 {
115f4a2713aSLionel Sambuc     static bool V;
116f4a2713aSLionel Sambuc };
117f4a2713aSLionel Sambuc 
118f4a2713aSLionel Sambuc extern template class S2<true>;
119f4a2713aSLionel Sambuc 
120f4a2713aSLionel Sambuc template <typename T>
121f4a2713aSLionel Sambuc struct S3 {
122f4a2713aSLionel Sambuc     void m();
123f4a2713aSLionel Sambuc };
124f4a2713aSLionel Sambuc 
125f4a2713aSLionel Sambuc template <typename T>
126f4a2713aSLionel Sambuc inline void S3<T>::m() { }
127f4a2713aSLionel Sambuc 
128f4a2713aSLionel Sambuc template <typename T>
129f4a2713aSLionel Sambuc struct S4 {
130f4a2713aSLionel Sambuc     void m() { }
131f4a2713aSLionel Sambuc };
132f4a2713aSLionel Sambuc extern template struct S4<int>;
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc void S4ImplicitInst() {
135f4a2713aSLionel Sambuc     S4<int> s;
136f4a2713aSLionel Sambuc     s.m();
137f4a2713aSLionel Sambuc }
138f4a2713aSLionel Sambuc 
139f4a2713aSLionel Sambuc struct S5 {
140f4a2713aSLionel Sambuc   S5(int x);
141f4a2713aSLionel Sambuc };
142f4a2713aSLionel Sambuc 
143f4a2713aSLionel Sambuc struct TS5 {
144f4a2713aSLionel Sambuc   S5 s;
145f4a2713aSLionel Sambuc   template <typename T>
146f4a2713aSLionel Sambuc   TS5(T y) : s(y) {}
147f4a2713aSLionel Sambuc };
148f4a2713aSLionel Sambuc 
149f4a2713aSLionel Sambuc // PR 8134
150f4a2713aSLionel Sambuc template<class T> void f_PR8134(T);
151f4a2713aSLionel Sambuc template<class T> void f_PR8134(T);
152f4a2713aSLionel Sambuc void g_PR8134() { f_PR8134(0); f_PR8134('x'); }
153f4a2713aSLionel Sambuc 
154f4a2713aSLionel Sambuc // rdar8580149
155f4a2713aSLionel Sambuc template <typename T>
156f4a2713aSLionel Sambuc struct S6;
157f4a2713aSLionel Sambuc 
158f4a2713aSLionel Sambuc template <typename T, unsigned N>
159f4a2713aSLionel Sambuc struct S6<const T [N]>
160f4a2713aSLionel Sambuc {
161f4a2713aSLionel Sambuc private:
162f4a2713aSLionel Sambuc    typedef const T t1[N];
163f4a2713aSLionel Sambuc public:
164f4a2713aSLionel Sambuc    typedef t1& t2;
165f4a2713aSLionel Sambuc };
166f4a2713aSLionel Sambuc 
167f4a2713aSLionel Sambuc template<typename T>
168f4a2713aSLionel Sambuc   struct S7;
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc template<unsigned N>
171f4a2713aSLionel Sambuc struct S7<int[N]> : S6<const int[N]> { };
172f4a2713aSLionel Sambuc 
173f4a2713aSLionel Sambuc // Zero-length template argument lists
174f4a2713aSLionel Sambuc namespace ZeroLengthExplicitTemplateArgs {
175f4a2713aSLionel Sambuc   template<typename T> void h();
176f4a2713aSLionel Sambuc 
177f4a2713aSLionel Sambuc   struct Y {
178f4a2713aSLionel Sambuc     template<typename T> void f();
179f4a2713aSLionel Sambuc   };
180f4a2713aSLionel Sambuc 
181f4a2713aSLionel Sambuc   template<typename T>
182f4a2713aSLionel Sambuc     void f(T *ptr) {
183f4a2713aSLionel Sambuc     T::template g<>(17);
184f4a2713aSLionel Sambuc     ptr->template g2<>(17);
185f4a2713aSLionel Sambuc     h<T>();
186f4a2713aSLionel Sambuc     h<int>();
187f4a2713aSLionel Sambuc     Y y;
188f4a2713aSLionel Sambuc     y.f<int>();
189f4a2713aSLionel Sambuc   }
190f4a2713aSLionel Sambuc 
191f4a2713aSLionel Sambuc   struct X {
192f4a2713aSLionel Sambuc     template<typename T> static void g(T);
193f4a2713aSLionel Sambuc     template<typename T> void g2(T);
194f4a2713aSLionel Sambuc   };
195f4a2713aSLionel Sambuc }
196f4a2713aSLionel Sambuc 
197f4a2713aSLionel Sambuc namespace NonTypeTemplateParmContext {
198f4a2713aSLionel Sambuc   template<typename T, int inlineCapacity = 0> class Vector { };
199f4a2713aSLionel Sambuc 
200f4a2713aSLionel Sambuc   struct String {
201f4a2713aSLionel Sambuc     template<int inlineCapacity>
202f4a2713aSLionel Sambuc     static String adopt(Vector<char, inlineCapacity>&);
203f4a2713aSLionel Sambuc   };
204f4a2713aSLionel Sambuc 
205f4a2713aSLionel Sambuc   template<int inlineCapacity>
206f4a2713aSLionel Sambuc     inline bool equalIgnoringNullity(const Vector<char, inlineCapacity>& a, const String& b) { return false; }
207f4a2713aSLionel Sambuc }
208f4a2713aSLionel Sambuc 
209f4a2713aSLionel Sambuc // <rdar://problem/11112464>
210f4a2713aSLionel Sambuc template< typename > class Foo;
211f4a2713aSLionel Sambuc 
212f4a2713aSLionel Sambuc template< typename T >
213f4a2713aSLionel Sambuc class Foo : protected T
214f4a2713aSLionel Sambuc {
215f4a2713aSLionel Sambuc  public:
216f4a2713aSLionel Sambuc   Foo& operator=( const Foo& other );
217f4a2713aSLionel Sambuc };
218f4a2713aSLionel Sambuc 
219f4a2713aSLionel Sambuc template<typename...A> struct NestedExpansion {
220f4a2713aSLionel Sambuc   template<typename...B> auto f(A...a, B...b) -> decltype(g(a + b...));
221f4a2713aSLionel Sambuc };
222f4a2713aSLionel Sambuc template struct NestedExpansion<char, char, char>;
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc namespace rdar13135282 {
225f4a2713aSLionel Sambuc template < typename _Alloc >
226f4a2713aSLionel Sambuc void foo(_Alloc = _Alloc());
227f4a2713aSLionel Sambuc 
228f4a2713aSLionel Sambuc template < bool > class __pool;
229f4a2713aSLionel Sambuc 
230f4a2713aSLionel Sambuc template < template < bool > class _PoolTp >
231f4a2713aSLionel Sambuc struct __common_pool {
232f4a2713aSLionel Sambuc   typedef _PoolTp < 0 > pool_type;
233f4a2713aSLionel Sambuc };
234f4a2713aSLionel Sambuc 
235f4a2713aSLionel Sambuc template < template < bool > class _PoolTp >
236f4a2713aSLionel Sambuc struct __common_pool_base : __common_pool < _PoolTp > {};
237f4a2713aSLionel Sambuc 
238f4a2713aSLionel Sambuc template < template < bool > class _PoolTp >
239f4a2713aSLionel Sambuc struct A : __common_pool_base < _PoolTp > {};
240f4a2713aSLionel Sambuc 
241f4a2713aSLionel Sambuc template < typename _Poolp = A < __pool > >
242f4a2713aSLionel Sambuc struct __mt_alloc {
243f4a2713aSLionel Sambuc   typedef typename _Poolp::pool_type __pool_type;
244f4a2713aSLionel Sambuc   __mt_alloc() {
245f4a2713aSLionel Sambuc     foo<__mt_alloc<> >();
246f4a2713aSLionel Sambuc   }
247f4a2713aSLionel Sambuc };
248f4a2713aSLionel Sambuc }
249f4a2713aSLionel Sambuc 
250f4a2713aSLionel Sambuc namespace PR13020 {
251f4a2713aSLionel Sambuc template<typename T>
252f4a2713aSLionel Sambuc void f() {
253f4a2713aSLionel Sambuc  enum E {
254f4a2713aSLionel Sambuc    enumerator
255f4a2713aSLionel Sambuc  };
256f4a2713aSLionel Sambuc 
257f4a2713aSLionel Sambuc  T t = enumerator;
258f4a2713aSLionel Sambuc }
259f4a2713aSLionel Sambuc 
260f4a2713aSLionel Sambuc template void f<int>();
261f4a2713aSLionel Sambuc }
262f4a2713aSLionel Sambuc 
263f4a2713aSLionel Sambuc template<typename T> void doNotDeserialize() {}
264f4a2713aSLionel Sambuc template<typename T> struct ContainsDoNotDeserialize {
265f4a2713aSLionel Sambuc   static int doNotDeserialize;
266f4a2713aSLionel Sambuc };
267f4a2713aSLionel Sambuc template<typename T> struct ContainsDoNotDeserialize2 {
268f4a2713aSLionel Sambuc   static void doNotDeserialize();
269f4a2713aSLionel Sambuc };
270f4a2713aSLionel Sambuc template<typename T> int ContainsDoNotDeserialize<T>::doNotDeserialize = 0;
271f4a2713aSLionel Sambuc template<typename T> void ContainsDoNotDeserialize2<T>::doNotDeserialize() {}
272f4a2713aSLionel Sambuc 
273f4a2713aSLionel Sambuc 
274f4a2713aSLionel Sambuc template<typename T> void DependentSpecializedFunc(T x) { x.foo(); }
275f4a2713aSLionel Sambuc template<typename T> class DependentSpecializedFuncClass {
276f4a2713aSLionel Sambuc   void foo() {}
277f4a2713aSLionel Sambuc   friend void DependentSpecializedFunc<>(DependentSpecializedFuncClass);
278f4a2713aSLionel Sambuc };
279f4a2713aSLionel Sambuc 
280f4a2713aSLionel Sambuc namespace cyclic_module_load {
281f4a2713aSLionel Sambuc   // Reduced from a libc++ modules crasher.
282f4a2713aSLionel Sambuc   namespace std {
283f4a2713aSLionel Sambuc     template<class> class mask_array;
284f4a2713aSLionel Sambuc     template<class> class valarray {
285f4a2713aSLionel Sambuc     public:
286f4a2713aSLionel Sambuc       valarray(const valarray &v);
287f4a2713aSLionel Sambuc     };
288f4a2713aSLionel Sambuc 
289f4a2713aSLionel Sambuc     class gslice {
290f4a2713aSLionel Sambuc       valarray<int> x;
291f4a2713aSLionel Sambuc       valarray<int> stride() const { return x; }
292f4a2713aSLionel Sambuc     };
293f4a2713aSLionel Sambuc 
294f4a2713aSLionel Sambuc     template<class> class mask_array {
295f4a2713aSLionel Sambuc       template<class> friend class valarray;
296f4a2713aSLionel Sambuc     };
297f4a2713aSLionel Sambuc   }
298f4a2713aSLionel Sambuc }
299f4a2713aSLionel Sambuc 
300f4a2713aSLionel Sambuc namespace local_extern {
301f4a2713aSLionel Sambuc   template<typename T> int f() {
302f4a2713aSLionel Sambuc     extern int arr[3];
303f4a2713aSLionel Sambuc     {
304f4a2713aSLionel Sambuc       extern T arr;
305f4a2713aSLionel Sambuc       return sizeof(arr);
306f4a2713aSLionel Sambuc     }
307f4a2713aSLionel Sambuc   }
308f4a2713aSLionel Sambuc   template<typename T> int g() {
309f4a2713aSLionel Sambuc     extern int arr[3];
310f4a2713aSLionel Sambuc     extern T arr;
311f4a2713aSLionel Sambuc     return sizeof(arr);
312f4a2713aSLionel Sambuc   }
313f4a2713aSLionel Sambuc }
314*0a6a1f1dSLionel Sambuc 
315*0a6a1f1dSLionel Sambuc namespace rdar15468709a {
316*0a6a1f1dSLionel Sambuc   template<typename> struct decay {};
317*0a6a1f1dSLionel Sambuc 
318*0a6a1f1dSLionel Sambuc   template<typename FooParamTy> auto foo(FooParamTy fooParam) -> decltype(fooParam);
319*0a6a1f1dSLionel Sambuc   template<typename BarParamTy> auto bar(BarParamTy barParam) -> decay<decltype(barParam)>;
320*0a6a1f1dSLionel Sambuc 
321*0a6a1f1dSLionel Sambuc   struct B {};
322*0a6a1f1dSLionel Sambuc 
323*0a6a1f1dSLionel Sambuc   void crash() {
324*0a6a1f1dSLionel Sambuc     B some;
325*0a6a1f1dSLionel Sambuc     bar(some);
326*0a6a1f1dSLionel Sambuc   }
327*0a6a1f1dSLionel Sambuc }
328*0a6a1f1dSLionel Sambuc 
329*0a6a1f1dSLionel Sambuc namespace rdar15468709b {
330*0a6a1f1dSLionel Sambuc   template<typename> struct decay {};
331*0a6a1f1dSLionel Sambuc 
332*0a6a1f1dSLionel Sambuc   template<typename... Foos> int returnsInt(Foos... foos);
333*0a6a1f1dSLionel Sambuc 
334*0a6a1f1dSLionel Sambuc   template<typename... FooParamTy> auto foo(FooParamTy... fooParam) -> decltype(returnsInt(fooParam...));
335*0a6a1f1dSLionel Sambuc   template<typename... BarParamTy> auto bar(BarParamTy... barParam) -> decay<decltype(returnsInt(barParam...))>;
336*0a6a1f1dSLionel Sambuc 
337*0a6a1f1dSLionel Sambuc   struct B {};
338*0a6a1f1dSLionel Sambuc 
339*0a6a1f1dSLionel Sambuc   void crash() {
340*0a6a1f1dSLionel Sambuc     B some;
341*0a6a1f1dSLionel Sambuc     bar(some);
342*0a6a1f1dSLionel Sambuc   }
343*0a6a1f1dSLionel Sambuc }
344*0a6a1f1dSLionel Sambuc 
345*0a6a1f1dSLionel Sambuc namespace rdar15468709c {
346*0a6a1f1dSLionel Sambuc   template<typename> struct decay {};
347*0a6a1f1dSLionel Sambuc 
348*0a6a1f1dSLionel Sambuc   template<class... Foos> int returnsInt(Foos... foos);
349*0a6a1f1dSLionel Sambuc 
350*0a6a1f1dSLionel Sambuc   template<typename FooParamTy> void foo(FooParamTy fooParam) { decltype(fooParam) a; }
351*0a6a1f1dSLionel Sambuc   template<typename BarParamTy> auto bar(BarParamTy barParam) -> decay<decltype(barParam)>;
352*0a6a1f1dSLionel Sambuc 
353*0a6a1f1dSLionel Sambuc   struct B {};
354*0a6a1f1dSLionel Sambuc 
355*0a6a1f1dSLionel Sambuc   void crash() {
356*0a6a1f1dSLionel Sambuc     B some;
357*0a6a1f1dSLionel Sambuc     bar(some);
358*0a6a1f1dSLionel Sambuc   }
359*0a6a1f1dSLionel Sambuc }
360*0a6a1f1dSLionel Sambuc 
361