Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -fsyntax-only %s |
Douglas Gregor | 940bd83 | 2009-03-13 21:04:12 +0000 | [diff] [blame] | 2 | typedef char one_byte; |
| 3 | typedef char (&two_bytes)[2]; |
| 4 | typedef char (&four_bytes)[4]; |
| 5 | typedef char (&eight_bytes)[8]; |
| 6 | |
| 7 | template<int N> struct A { }; |
| 8 | |
| 9 | namespace N1 { |
| 10 | struct X { }; |
| 11 | } |
| 12 | |
| 13 | namespace N2 { |
| 14 | struct Y { }; |
| 15 | |
| 16 | two_bytes operator+(Y, Y); |
| 17 | } |
| 18 | |
| 19 | namespace N3 { |
| 20 | struct Z { }; |
| 21 | |
| 22 | eight_bytes operator+(Z, Z); |
| 23 | } |
| 24 | |
| 25 | namespace N4 { |
| 26 | one_byte operator+(N1::X, N2::Y); |
| 27 | |
| 28 | template<typename T, typename U> |
| 29 | struct BinOpOverload { |
| 30 | typedef A<sizeof(T() + U())> type; |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | namespace N1 { |
| 35 | four_bytes operator+(X, X); |
| 36 | } |
| 37 | |
| 38 | namespace N3 { |
| 39 | eight_bytes operator+(Z, Z); // redeclaration |
| 40 | } |
| 41 | |
| 42 | void test_bin_op_overload(A<1> *a1, A<2> *a2, A<4> *a4, A<8> *a8) { |
| 43 | typedef N4::BinOpOverload<N1::X, N2::Y>::type XY; |
| 44 | XY *xy = a1; |
| 45 | typedef N4::BinOpOverload<N1::X, N1::X>::type XX; |
| 46 | XX *xx = a4; |
| 47 | typedef N4::BinOpOverload<N2::Y, N2::Y>::type YY; |
| 48 | YY *yy = a2; |
| 49 | typedef N4::BinOpOverload<N3::Z, N3::Z>::type ZZ; |
| 50 | ZZ *zz = a8; |
| 51 | } |
| 52 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 53 | namespace N3 { |
| 54 | eight_bytes operator-(::N3::Z); |
| 55 | } |
| 56 | |
| 57 | namespace N4 { |
| 58 | template<typename T> |
| 59 | struct UnaryOpOverload { |
| 60 | typedef A<sizeof(-T())> type; |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | void test_unary_op_overload(A<8> *a8) { |
| 65 | typedef N4::UnaryOpOverload<N3::Z>::type UZ; |
| 66 | UZ *uz = a8; |
| 67 | } |
Gabor Greif | 087edcf | 2009-03-18 00:55:04 +0000 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | namespace N5 { |
| 71 | template<int I> |
| 72 | struct Lookup { |
| 73 | enum { val = I, more = val + 1 }; |
| 74 | }; |
| 75 | |
| 76 | template<bool B> |
| 77 | struct Cond { |
| 78 | enum Junk { is = B ? Lookup<B>::more : Lookup<Lookup<B+1>::more>::val }; |
| 79 | }; |
| 80 | |
| 81 | enum { resultT = Cond<true>::is, |
| 82 | resultF = Cond<false>::is }; |
| 83 | } |
| 84 | */ |
| 85 | |
| 86 | namespace N6 { |
Gabor Greif | a88620c | 2009-03-18 20:26:44 +0000 | [diff] [blame] | 87 | // non-typedependent |
Gabor Greif | 087edcf | 2009-03-18 00:55:04 +0000 | [diff] [blame] | 88 | template<int I> |
Gabor Greif | a88620c | 2009-03-18 20:26:44 +0000 | [diff] [blame] | 89 | struct Lookup {}; |
Gabor Greif | 087edcf | 2009-03-18 00:55:04 +0000 | [diff] [blame] | 90 | |
| 91 | template<bool B, typename T, typename E> |
| 92 | struct Cond { |
| 93 | typedef Lookup<B ? sizeof(T) : sizeof(E)> True; |
| 94 | typedef Lookup<!B ? sizeof(T) : sizeof(E)> False; |
| 95 | }; |
| 96 | |
| 97 | typedef Cond<true, int, char>::True True; |
Gabor Greif | 6c473c8 | 2009-03-18 01:16:08 +0000 | [diff] [blame] | 98 | typedef Cond<true, int, char>::False False; |
| 99 | |
| 100 | // check that we have the right types |
| 101 | Lookup<1> const &L1(False()); |
| 102 | Lookup<sizeof(int)> const &L2(True()); |
Gabor Greif | 087edcf | 2009-03-18 00:55:04 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
Gabor Greif | a88620c | 2009-03-18 20:26:44 +0000 | [diff] [blame] | 106 | namespace N7 { |
| 107 | // type dependent |
| 108 | template<int I> |
| 109 | struct Lookup {}; |
| 110 | |
| 111 | template<bool B, typename T, typename E> |
| 112 | struct Cond { |
| 113 | T foo() { return B ? T() : E(); } |
| 114 | typedef Lookup<sizeof(B ? T() : E())> Type; |
| 115 | }; |
| 116 | |
| 117 | //Cond<true, int*, double> C; // Errors |
| 118 | //int V(C.foo()); // Errors |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 119 | //typedef Cond<true, int*, double>::Type Type; // Errors |
Gabor Greif | a88620c | 2009-03-18 20:26:44 +0000 | [diff] [blame] | 120 | typedef Cond<true, int, double>::Type Type; |
| 121 | } |
| 122 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 123 | template<typename T, unsigned long N> struct IntegralConstant { }; |
| 124 | |
| 125 | template<typename T> |
| 126 | struct X0 { |
| 127 | void f(T x, IntegralConstant<T, sizeof(x)>); |
| 128 | }; |
| 129 | |
| 130 | void test_X0(X0<int> x, IntegralConstant<int, sizeof(int)> ic) { |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 131 | x.f(5,ic); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 132 | } |
Douglas Gregor | 017dde5 | 2009-08-31 20:00:26 +0000 | [diff] [blame] | 133 | |
| 134 | namespace N8 { |
| 135 | struct X { |
| 136 | X operator+(const X&) const; |
| 137 | }; |
| 138 | |
| 139 | template<typename T> |
| 140 | T test_plus(const T* xp, const T& x, const T& y) { |
| 141 | x.operator+(y); |
| 142 | return xp->operator+(y); |
| 143 | } |
| 144 | |
| 145 | void test_test_plus(X x) { |
| 146 | test_plus(&x, x, x); |
| 147 | } |
| 148 | } |
Douglas Gregor | 8f1d89e | 2009-09-01 16:58:52 +0000 | [diff] [blame] | 149 | |
| 150 | namespace N9 { |
| 151 | struct A { |
| 152 | bool operator==(int value); |
| 153 | }; |
| 154 | |
| 155 | template<typename T> struct B { |
| 156 | bool f(A a) { |
| 157 | return a == 1; |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | template struct B<int>; |
| 162 | } |
Douglas Gregor | 44c7384 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 163 | |
| 164 | namespace N10 { |
| 165 | template <typename T> |
| 166 | class A { |
| 167 | struct X { }; |
| 168 | |
| 169 | public: |
| 170 | ~A() { |
| 171 | f(reinterpret_cast<X *>(0), reinterpret_cast<X *>(0)); |
| 172 | } |
| 173 | |
| 174 | private: |
| 175 | void f(X *); |
| 176 | void f(X *, X *); |
| 177 | }; |
| 178 | |
| 179 | template class A<int>; |
| 180 | } |
Douglas Gregor | 089407b | 2009-10-17 21:40:42 +0000 | [diff] [blame] | 181 | |
| 182 | namespace N12 { |
| 183 | // PR5224 |
| 184 | template<typename T> |
| 185 | struct A { typedef int t0; }; |
| 186 | |
| 187 | struct C { |
| 188 | C(int); |
| 189 | |
| 190 | template<typename T> |
| 191 | static C *f0(T a0) {return new C((typename A<T>::t0) 1); } |
| 192 | }; |
| 193 | |
| 194 | void f0(int **a) { C::f0(a); } |
| 195 | } |