alias-templates.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
  2. template<typename S>
  3. struct A {
  4. typedef S B;
  5. template<typename T> using C = typename T::B;
  6. template<typename T> struct D {
  7. template<typename U> using E = typename A<U>::template C<A<T>>;
  8. template<typename U> using F = A<E<U>>;
  9. template<typename U> using G = C<F<U>>;
  10. G<T> g;
  11. };
  12. typedef decltype(D<B>().g) H;
  13. D<H> h;
  14. template<typename T> using I = A<decltype(h.g)>;
  15. template<typename T> using J = typename A<decltype(h.g)>::template C<I<T>>;
  16. };
  17. A<int> a;
  18. A<char>::D<double> b;
  19. template<typename T> T make();
  20. namespace X {
  21. template<typename T> struct traits {
  22. typedef T thing;
  23. typedef decltype(val(make<thing>())) inner_ptr;
  24. template<typename U> using rebind_thing = typename thing::template rebind<U>;
  25. template<typename U> using rebind = traits<rebind_thing<U>>;
  26. inner_ptr &&alloc();
  27. void free(inner_ptr&&);
  28. };
  29. template<typename T> struct ptr_traits {
  30. typedef T *type;
  31. };
  32. template<typename T> using ptr = typename ptr_traits<T>::type;
  33. template<typename T> struct thing {
  34. typedef T inner;
  35. typedef ptr<inner> inner_ptr;
  36. typedef traits<thing<inner>> traits_type;
  37. template<typename U> using rebind = thing<U>;
  38. thing(traits_type &traits) : traits(traits), val(traits.alloc()) {}
  39. ~thing() { traits.free(static_cast<inner_ptr&&>(val)); }
  40. traits_type &traits;
  41. inner_ptr val;
  42. friend inner_ptr val(const thing &t) { return t.val; }
  43. };
  44. template<> struct ptr_traits<bool> {
  45. typedef bool &type;
  46. };
  47. template<> bool &traits<thing<bool>>::alloc() { static bool b; return b; }
  48. template<> void traits<thing<bool>>::free(bool&) {}
  49. }
  50. typedef X::traits<X::thing<int>> itt;
  51. itt::thing::traits_type itr;
  52. itt::thing ith(itr);
  53. itt::rebind<bool> btr;
  54. itt::rebind_thing<bool> btt(btr);
  55. namespace PR11848 {
  56. template<typename T> using U = int;
  57. template<typename T, typename ...Ts>
  58. void f1(U<T> i, U<Ts> ...is) { // expected-note 2{{couldn't infer template argument 'T'}}
  59. return i + f1<Ts...>(is...);
  60. }
  61. // FIXME: This note is technically correct, but could be better. We
  62. // should really say that we couldn't infer template argument 'Ts'.
  63. template<typename ...Ts>
  64. void f2(U<Ts> ...is) { } // expected-note {{requires 0 arguments, but 1 was provided}}
  65. template<typename...> struct type_tuple {};
  66. template<typename ...Ts>
  67. void f3(type_tuple<Ts...>, U<Ts> ...is) {} // expected-note {{requires 4 arguments, but 3 were provided}}
  68. void g() {
  69. f1(U<void>()); // expected-error {{no match}}
  70. f1(1, 2, 3, 4, 5); // expected-error {{no match}}
  71. f2(); // ok
  72. f2(1); // expected-error {{no match}}
  73. f3(type_tuple<>());
  74. f3(type_tuple<void, void, void>(), 1, 2); // expected-error {{no match}}
  75. f3(type_tuple<void, void, void>(), 1, 2, 3);
  76. }
  77. template<typename ...Ts>
  78. struct S {
  79. S(U<Ts>...ts);
  80. };
  81. template<typename T>
  82. struct Hidden1 {
  83. template<typename ...Ts>
  84. Hidden1(typename T::template U<Ts> ...ts);
  85. };
  86. template<typename T, typename ...Ts>
  87. struct Hidden2 {
  88. Hidden2(typename T::template U<Ts> ...ts);
  89. };
  90. struct Hide {
  91. template<typename T> using U = int;
  92. };
  93. Hidden1<Hide> h1;
  94. Hidden2<Hide, double, char> h2(1, 2);
  95. }
  96. namespace Core22036 {
  97. struct X {};
  98. void h(...);
  99. template<typename T> using Y = X;
  100. template<typename T, typename ...Ts> struct S {
  101. // An expression can contain an unexpanded pack without being type or
  102. // value dependent. This is true even if the expression's type is a pack
  103. // expansion type.
  104. void f1(Y<T> a) { h(g(a)); } // expected-error {{undeclared identifier 'g'}}
  105. void f2(Y<Ts>...as) { h(g(as)...); } // expected-error {{undeclared identifier 'g'}}
  106. void f3(Y<Ts>...as) { g(as...); } // ok
  107. void f4(Ts ...ts) { h(g(sizeof(ts))...); } // expected-error {{undeclared identifier 'g'}}
  108. // FIXME: We can reject this, since it has no valid instantiations because
  109. // 'g' never has any associated namespaces.
  110. void f5(Ts ...ts) { g(sizeof(ts)...); } // ok
  111. };
  112. }
  113. namespace PR13243 {
  114. template<typename A> struct X {};
  115. template<int I> struct C {};
  116. template<int I> using Ci = C<I>;
  117. template<typename A, int I> void f(X<A>, Ci<I>) {}
  118. template void f(X<int>, C<0>);
  119. }
  120. namespace PR13136 {
  121. template <typename T, T... Numbers>
  122. struct NumberTuple { };
  123. template <unsigned int... Numbers>
  124. using MyNumberTuple = NumberTuple<unsigned int, Numbers...>;
  125. template <typename U, unsigned int... Numbers>
  126. void foo(U&&, MyNumberTuple<Numbers...>);
  127. template <typename U, unsigned int... Numbers>
  128. void bar(U&&, NumberTuple<unsigned int, Numbers...>);
  129. int main() {
  130. foo(1, NumberTuple<unsigned int, 0, 1>());
  131. bar(1, NumberTuple<unsigned int, 0, 1>());
  132. return 0;
  133. }
  134. }
  135. namespace PR16646 {
  136. namespace test1 {
  137. template <typename T> struct DefaultValue { const T value=0;};
  138. template <typename ... Args> struct tuple {};
  139. template <typename ... Args> using Zero = tuple<DefaultValue<Args> ...>;
  140. template <typename ... Args> void f(const Zero<Args ...> &t);
  141. void f() {
  142. f(Zero<int,double,double>());
  143. }
  144. }
  145. namespace test2 {
  146. template<int x> struct X {};
  147. template <template<int x> class temp> struct DefaultValue { const temp<0> value; };
  148. template <typename ... Args> struct tuple {};
  149. template <template<int x> class... Args> using Zero = tuple<DefaultValue<Args> ...>;
  150. template <template<int x> class... Args> void f(const Zero<Args ...> &t);
  151. void f() {
  152. f(Zero<X,X,X>());
  153. }
  154. }
  155. }
  156. namespace PR16904 {
  157. template <typename,typename>
  158. struct base {
  159. template <typename> struct derived;
  160. };
  161. // FIXME: The diagnostics here are terrible.
  162. template <typename T, typename U, typename V>
  163. using derived = base<T, U>::template derived<V>; // expected-error {{expected a type}} expected-error {{expected ';'}}
  164. template <typename T, typename U, typename V>
  165. using derived2 = ::PR16904::base<T, U>::template derived<V>; // expected-error {{expected a type}} expected-error {{expected ';'}}
  166. }