instantiate-expr-2.cpp 4.1 KB

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