cxx1z-using-declaration.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // RUN: %clang_cc1 -std=c++1z -verify %s
  2. // Test that we cope with failure to expand a pack.
  3. template<typename ...T> struct Unexpanded : T... {
  4. using T::f; // expected-error {{unexpanded}}
  5. using typename T::type; // expected-error {{unexpanded}}
  6. template<typename ...U> void g(U ...u) { f(u...); } // expected-error {{undeclared identifier 'f'}}
  7. void h() {
  8. Unexpanded<type...> *p; // expected-error {{undeclared identifier 'type'}}
  9. }
  10. };
  11. void test_Unexpanded() {
  12. struct A { void f(); }; // expected-note {{must qualify}}
  13. struct B { void f(int); }; // expected-note {{must qualify}}
  14. Unexpanded<A, B>().g(0); // expected-note {{instantiation of}}
  15. }
  16. // Test using non-type members from pack of base classes.
  17. template<typename ...T> struct A : T... { // expected-note 2{{candidate}}
  18. using T::T ...; // expected-note 6{{inherited here}}
  19. using T::operator() ...;
  20. using T::operator T* ...;
  21. using T::h ...;
  22. void f(int n) { h(n); } // expected-error {{ambiguous}}
  23. void f(int n, int m) { h(n, m); } // expected-error {{member using declaration 'h' instantiates to an empty pack}}
  24. void g(int n) { (*this)(n); } // expected-error {{ambiguous}}
  25. void g(int n, int m) { (*this)(n, m); } // expected-error {{does not provide a call operator}}
  26. };
  27. namespace test_A {
  28. struct X { // expected-note 2{{candidate}}
  29. X();
  30. X(int); // expected-note {{candidate}}
  31. void operator()(int); // expected-note 2{{candidate}}
  32. operator X *();
  33. void h(int); // expected-note {{candidate}}
  34. };
  35. struct Y {
  36. Y();
  37. Y(int, int);
  38. void operator()(int, int);
  39. operator Y *();
  40. void h(int, int); // expected-note {{not viable}}
  41. };
  42. struct Z { // expected-note 2{{candidate}}
  43. Z();
  44. Z(int); // expected-note {{candidate}}
  45. void operator()(int); // expected-note 2{{candidate}}
  46. operator Z *();
  47. void h(int); // expected-note {{candidate}}
  48. };
  49. void f() {
  50. A<> a;
  51. a.f(0, 0); // expected-note {{instantiation of}}
  52. a.g(0, 0); // expected-note {{instantiation of}}
  53. A<X, Y> axy(0);
  54. A<X, Y>(0, 0);
  55. axy.f(0);
  56. axy.f(0, 0);
  57. axy.g(0);
  58. axy.g(0, 0);
  59. axy(0);
  60. axy(0, 0);
  61. A<X, Y, Z>(0); // expected-error {{ambiguous}}
  62. A<X, Y, Z> axyz(0, 0);
  63. axyz.f(0); // expected-note {{instantiation of}}
  64. axyz.f(0, 0);
  65. axyz.g(0); // expected-note {{instantiation of}}
  66. axyz.g(0, 0);
  67. axyz(0); // expected-error {{ambiguous}}
  68. axyz(0, 0);
  69. X *x;
  70. x = a; // expected-error {{incompatible}}
  71. x = axy;
  72. x = axyz;
  73. x = a.operator X*(); // expected-error {{no member}}
  74. x = axy.operator X*();
  75. x = axyz.operator X*();
  76. Z *z;
  77. z = axyz;
  78. z = axyz.operator Z*();
  79. }
  80. }
  81. // Test using pack of non-type members from single base class.
  82. template<typename X, typename Y, typename ...T> struct B : X, Y {
  83. using X::operator T* ...;
  84. };
  85. namespace test_B {
  86. struct X { operator int*(); operator float*(); operator char*(); }; // expected-note {{candidate}}
  87. struct Y { operator int*(); operator float*(); operator char*(); }; // expected-note {{candidate}}
  88. B<X, Y, int, float> bif;
  89. int *pi = bif;
  90. float *pf = bif;
  91. char *pc = bif; // expected-error {{ambiguous}}
  92. }
  93. // Test using type member from pack of base classes.
  94. template<typename ...T> struct C : T... {
  95. using typename T::type ...; // expected-error {{target of using declaration conflicts}}
  96. void f() { type value; } // expected-error {{member using declaration 'type' instantiates to an empty pack}}
  97. };
  98. namespace test_C {
  99. struct X { typedef int type; };
  100. struct Y { typedef int type; }; // expected-note {{conflicting}}
  101. struct Z { typedef float type; }; // expected-note {{target}}
  102. void f() {
  103. C<> c;
  104. c.f(); // expected-note {{instantiation of}}
  105. C<X, Y> cxy;
  106. cxy.f();
  107. C<X, Y, Z> cxyz; // expected-note {{instantiation of}}
  108. cxyz.f();
  109. }
  110. }
  111. // Test using pack of non-types at block scope.
  112. template<typename ...T> int fn1() {
  113. using T::e ...; // expected-error 2{{class member}} expected-note 2{{instead}}
  114. // expected-error@-1 2{{produces multiple values}}
  115. return e; // expected-error {{using declaration 'e' instantiates to an empty pack}}
  116. }
  117. namespace test_fn1 {
  118. struct X { static int e; };
  119. struct Y { typedef int e; };
  120. inline namespace P { enum E { e }; }
  121. inline namespace Q { enum F { e }; }
  122. void f() {
  123. fn1<>(); // expected-note {{instantiation of}}
  124. fn1<X>(); // expected-note {{instantiation of}}
  125. fn1<Y>(); // expected-note {{instantiation of}}
  126. fn1<E>();
  127. fn1<E, F>(); // expected-note {{instantiation of}}
  128. fn1<E, X>(); // expected-note {{instantiation of}}
  129. }
  130. }
  131. // Test using pack of types at block scope.
  132. template<typename ...T> void fn2() {
  133. // This cannot ever be valid: in order for T::type to be a type, T must be a
  134. // class, and a class member cannot be named by a block-scope using declaration.
  135. using typename T::type ...; // expected-error {{class member}}
  136. type x; // expected-error {{unknown type name 'type'}}
  137. }
  138. // Test partial substitution into class-scope pack.
  139. template<typename ...T> auto lambda1() {
  140. return [](auto x) {
  141. struct A : T::template X<decltype(x)>... { // expected-note 1+{{instantiation of}}
  142. using T::template X<decltype(x)>::f ...;
  143. using typename T::template X<decltype(x)>::type ...;
  144. void g(int n) { f(n); } // expected-error {{empty pack}} expected-error {{expected 2, have 1}} expected-error {{ambiguous}}
  145. void h() { type value; } // expected-error {{empty pack}}
  146. };
  147. return A();
  148. };
  149. }
  150. namespace test_lambda1 {
  151. struct A {
  152. template<typename> struct X {
  153. void f(int); // expected-note {{candidate}}
  154. using type = int;
  155. };
  156. };
  157. struct B {
  158. template<typename> struct X {
  159. void f(int, int); // expected-note {{declared here}} expected-note {{not viable}}
  160. using type = int;
  161. };
  162. };
  163. struct C {
  164. template<typename> struct X {
  165. void f(int); // expected-note {{candidate}}
  166. void f(int, int); // expected-note {{not viable}}
  167. using type = int;
  168. };
  169. };
  170. void f() {
  171. lambda1<>() // expected-note 2{{instantiation of}}
  172. (0)
  173. // FIXME: This is poor error recovery
  174. .g(0); // expected-error {{no member named 'g'}}
  175. lambda1<A>()
  176. (0)
  177. .g(0);
  178. lambda1<B>()
  179. (0) // expected-note {{instantiation of}}
  180. .g(0);
  181. lambda1<A, B, C>()
  182. (0) // expected-note {{instantiation of}}
  183. .g(0);
  184. }
  185. }
  186. namespace p0195r2_example {
  187. template<typename ...Ts>
  188. struct Overloader : Ts... {
  189. using Ts::operator() ...;
  190. };
  191. template<typename ...Ts>
  192. constexpr auto make_overloader(Ts &&...ts) {
  193. return Overloader<Ts...>{static_cast<Ts&&>(ts)...};
  194. }
  195. void test() {
  196. auto o = make_overloader(
  197. [&](int &r) -> int & { return r; }, // expected-note {{candidate function}}
  198. [&](float &r) -> float & { return r; } // expected-note {{candidate function}}
  199. );
  200. int a; float f; double d;
  201. int &ra = o(a);
  202. float &rf = o(f);
  203. double &rd = o(d); // expected-error {{no matching function}}
  204. }
  205. }