Daniel Dunbar | 8fbe78f | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Douglas Gregor | f8f8683 | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2 | template<typename T, int N = 2> struct X; // expected-note{{template is declared here}} |
| 3 | |
| 4 | X<int, 1> *x1; |
| 5 | X<int> *x2; |
| 6 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 7 | X<> *x3; // expected-error{{too few template arguments for class template 'X'}} |
Douglas Gregor | f8f8683 | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 8 | |
| 9 | template<typename U = float, int M> struct X; |
| 10 | |
| 11 | X<> *x4; |
Anders Carlsson | dd096d8 | 2009-06-05 02:12:32 +0000 | [diff] [blame] | 12 | |
Anders Carlsson | 03c9e87 | 2009-06-05 02:45:24 +0000 | [diff] [blame] | 13 | template<typename T = int> struct Z { }; |
Anders Carlsson | dd096d8 | 2009-06-05 02:12:32 +0000 | [diff] [blame] | 14 | template struct Z<>; |
Anders Carlsson | 40ed344 | 2009-06-11 16:06:49 +0000 | [diff] [blame] | 15 | |
| 16 | // PR4362 |
| 17 | template<class T> struct a { }; |
| 18 | template<> struct a<int> { static const bool v = true; }; |
| 19 | |
| 20 | template<class T, bool = a<T>::v> struct p { }; // expected-error {{no member named 'v'}} |
| 21 | |
| 22 | template struct p<bool>; // expected-note {{in instantiation of default argument for 'p<bool>' required here}} |
| 23 | template struct p<int>; |
Douglas Gregor | 568a071 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 24 | |
| 25 | // PR5187 |
| 26 | template<typename T, typename U> |
| 27 | struct A; |
| 28 | |
| 29 | template<typename T, typename U = T> |
| 30 | struct A; |
| 31 | |
| 32 | template<typename T, typename U> |
| 33 | struct A { |
| 34 | void f(A<T>); |
| 35 | }; |
| 36 | |
| 37 | template<typename T> |
| 38 | struct B { }; |
| 39 | |
| 40 | template<> |
| 41 | struct B<void> { |
| 42 | typedef B<void*> type; |
| 43 | }; |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 44 | |
| 45 | // Nested default arguments for template parameters. |
| 46 | template<typename T> struct X1 { }; |
| 47 | |
| 48 | template<typename T> |
| 49 | struct X2 { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 50 | template<typename U = typename X1<T>::type> // expected-error{{no type named 'type' in 'X1<int>'}} \ |
| 51 | // expected-error{{no type named 'type' in 'X1<char>'}} |
| 52 | struct Inner1 { }; // expected-note{{template is declared here}} |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 53 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 54 | template<T Value = X1<T>::value> // expected-error{{no member named 'value' in 'X1<int>'}} \ |
| 55 | // expected-error{{no member named 'value' in 'X1<char>'}} |
| 56 | struct NonType1 { }; // expected-note{{template is declared here}} |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 57 | |
| 58 | template<T Value> |
| 59 | struct Inner2 { }; |
| 60 | |
| 61 | template<typename U> |
| 62 | struct Inner3 { |
| 63 | template<typename X = T, typename V = U> |
| 64 | struct VeryInner { }; |
| 65 | |
| 66 | template<T Value1 = sizeof(T), T Value2 = sizeof(U), |
| 67 | T Value3 = Value1 + Value2> |
| 68 | struct NonType2 { }; |
| 69 | }; |
| 70 | }; |
| 71 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 72 | X2<int> x2i; // expected-note{{in instantiation of template class 'X2<int>' requested here}} |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 73 | X2<int>::Inner1<float> x2iif; |
| 74 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 75 | X2<int>::Inner1<> x2bad; // expected-error{{too few template arguments for class template 'Inner1'}} |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 76 | |
| 77 | X2<int>::NonType1<'a'> x2_nontype1; |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 78 | X2<int>::NonType1<> x2_nontype1_bad; // expected-error{{too few template arguments for class template 'NonType1'}} |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 79 | |
| 80 | // Check multi-level substitution into template type arguments |
| 81 | X2<int>::Inner3<float>::VeryInner<> vi; |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 82 | X2<char>::Inner3<int>::NonType2<> x2_deep_nontype; // expected-note{{in instantiation of template class 'X2<char>' requested here}} |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 83 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 84 | template<typename T, typename U> |
| 85 | struct is_same { static const bool value = false; }; |
| 86 | |
| 87 | template<typename T> |
| 88 | struct is_same<T, T> { static const bool value = true; }; |
| 89 | |
Douglas Gregor | 120635b | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 90 | int array1[is_same<__typeof__(vi), |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 91 | X2<int>::Inner3<float>::VeryInner<int, float> >::value? 1 : -1]; |
| 92 | |
Douglas Gregor | 120635b | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 93 | int array2[is_same<__typeof(x2_deep_nontype), |
| 94 | X2<char>::Inner3<int>::NonType2<sizeof(char), sizeof(int), |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 95 | sizeof(char)+sizeof(int)> >::value? 1 : -1]; |
Douglas Gregor | 120635b | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 96 | |
| 97 | // Template template parameter defaults |
| 98 | template<template<typename T> class X = X2> struct X3 { }; |
| 99 | int array3[is_same<X3<>, X3<X2> >::value? 1 : -1]; |
| 100 | |
| 101 | struct add_pointer { |
| 102 | template<typename T> |
| 103 | struct apply { |
| 104 | typedef T* type; |
| 105 | }; |
| 106 | }; |
| 107 | |
| 108 | template<typename T, template<typename> class X = T::template apply> |
| 109 | struct X4; |
| 110 | int array4[is_same<X4<add_pointer>, |
| 111 | X4<add_pointer, add_pointer::apply> >::value? 1 : -1]; |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 112 | |
| 113 | template<int> struct X5 {}; // expected-note{{has a different type 'int'}} |
| 114 | template<long> struct X5b {}; |
| 115 | template<typename T, |
| 116 | template<T> class B = X5> // expected-error{{template template argument has different}} \ |
| 117 | // expected-note{{previous non-type template parameter}} |
| 118 | struct X6 {}; |
| 119 | |
| 120 | X6<int> x6a; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 121 | X6<long> x6b; // expected-note{{while checking a default template argument}} |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 122 | X6<long, X5b> x6c; |
Douglas Gregor | c998409 | 2009-11-12 00:03:40 +0000 | [diff] [blame] | 123 | |
| 124 | |
| 125 | template<template<class> class X = B<int> > struct X7; // expected-error{{must be a class template}} |
Douglas Gregor | 5546262 | 2011-06-15 14:20:42 +0000 | [diff] [blame] | 126 | |
| 127 | namespace PR9643 { |
| 128 | template<typename T> class allocator {}; |
| 129 | template<typename T, typename U = allocator<T> > class vector {}; |
| 130 | |
| 131 | template<template<typename U, typename = allocator<U> > class container, |
| 132 | typename DT> |
| 133 | container<DT> initializer(const DT& d) { |
| 134 | return container<DT>(); |
| 135 | } |
| 136 | |
| 137 | void f() { |
| 138 | vector<int, allocator<int> > v = initializer<vector>(5); |
| 139 | } |
| 140 | } |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 141 | |
| 142 | namespace PR16288 { |
| 143 | template<typename X> |
| 144 | struct S { |
| 145 | template<typename T = int, typename U> // expected-warning {{C++11}} |
| 146 | void f(); |
| 147 | }; |
| 148 | template<typename X> |
| 149 | template<typename T, typename U> |
| 150 | void S<X>::f() {} |
| 151 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 152 | |
| 153 | namespace DR1635 { |
| 154 | template <class T> struct X { |
| 155 | template <class U = typename T::type> static void f(int) {} // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} \ |
| 156 | // expected-warning {{C++11}} |
| 157 | static void f(...) {} |
| 158 | }; |
| 159 | |
| 160 | int g() { X<int>::f(0); } // expected-note {{in instantiation of template class 'DR1635::X<int>' requested here}} |
| 161 | } |
Serge Pavlov | 73c6a24 | 2015-08-23 10:22:28 +0000 | [diff] [blame] | 162 | |
| 163 | namespace NondefDecls { |
| 164 | template<typename T> void f1() { |
| 165 | int g1(int defarg = T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}} |
| 166 | } |
| 167 | template void f1<int>(); // expected-note{{in instantiation of function template specialization 'NondefDecls::f1<int>' requested here}} |
| 168 | } |