lambda-expressions.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // RUN: %clang_cc1 -std=c++0x -Wno-unused-value -fsyntax-only -verify -fblocks %s
  2. namespace std { class type_info; };
  3. namespace ExplicitCapture {
  4. class C {
  5. int Member;
  6. static void Overload(int);
  7. void Overload();
  8. virtual C& Overload(float);
  9. void ImplicitThisCapture() {
  10. [](){(void)Member;}; // expected-error {{'this' cannot be implicitly captured in this context}}
  11. [&](){(void)Member;};
  12. [this](){(void)Member;};
  13. [this]{[this]{};};
  14. []{[this]{};};// expected-error {{'this' cannot be implicitly captured in this context}}
  15. []{Overload(3);};
  16. []{Overload();}; // expected-error {{'this' cannot be implicitly captured in this context}}
  17. []{(void)typeid(Overload());};
  18. []{(void)typeid(Overload(.5f));};// expected-error {{'this' cannot be implicitly captured in this context}}
  19. }
  20. };
  21. void f() {
  22. [this] () {}; // expected-error {{'this' cannot be captured in this context}}
  23. }
  24. }
  25. namespace ReturnDeduction {
  26. void test() {
  27. [](){ return 1; };
  28. [](){ return 1; };
  29. [](){ return ({return 1; 1;}); };
  30. [](){ return ({return 'c'; 1;}); }; // expected-error {{must match previous return type}}
  31. []()->int{ return 'c'; return 1; };
  32. [](){ return 'c'; return 1; }; // expected-error {{must match previous return type}}
  33. []() { return; return (void)0; };
  34. [](){ return 1; return 1; };
  35. }
  36. }
  37. namespace ImplicitCapture {
  38. void test() {
  39. int a = 0; // expected-note 5 {{declared}}
  40. []() { return a; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{begins here}}
  41. [&]() { return a; };
  42. [=]() { return a; };
  43. [=]() { int* b = &a; }; // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const int *'}}
  44. [=]() { return [&]() { return a; }; };
  45. []() { return [&]() { return a; }; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
  46. []() { return ^{ return a; }; };// expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
  47. []() { return [&a] { return a; }; }; // expected-error 2 {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note 2 {{lambda expression begins here}}
  48. [=]() { return [&a] { return a; }; }; //
  49. const int b = 2;
  50. []() { return b; };
  51. union { // expected-note {{declared}}
  52. int c;
  53. float d;
  54. };
  55. d = 3;
  56. [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
  57. __block int e; // expected-note 3 {{declared}}
  58. [&]() { return e; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}}
  59. [&e]() { return e; }; // expected-error 2 {{__block variable 'e' cannot be captured in a lambda expression}}
  60. int f[10]; // expected-note {{declared}}
  61. [&]() { return f[2]; };
  62. (void) ^{ return []() { return f[2]; }; }; // expected-error {{variable 'f' cannot be implicitly captured in a lambda with no capture-default specified}} \
  63. // expected-note{{lambda expression begins here}}
  64. struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}}
  65. G g;
  66. [=]() { const G* gg = &g; return gg->a; };
  67. [=]() { return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error {{no matching constructor for initialization of 'ImplicitCapture::G'}}
  68. (void)^{ return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error 2 {{no matching constructor for initialization of 'const ImplicitCapture::G'}}
  69. const int h = a; // expected-note {{declared}}
  70. []() { return h; }; // expected-error {{variable 'h' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
  71. // The exemption for variables which can appear in constant expressions
  72. // applies only to objects (and not to references).
  73. // FIXME: This might be a bug in the standard.
  74. static int i;
  75. constexpr int &ref_i = i; // expected-note {{declared}}
  76. [] { return ref_i; }; // expected-error {{variable 'ref_i' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
  77. }
  78. }
  79. namespace PR12031 {
  80. struct X {
  81. template<typename T>
  82. X(const T&);
  83. ~X();
  84. };
  85. void f(int i, X x);
  86. void g() {
  87. const int v = 10;
  88. f(v, [](){});
  89. }
  90. }
  91. namespace NullPtr {
  92. int &f(int *p);
  93. char &f(...);
  94. void g() {
  95. int n = 0;
  96. [=] {
  97. char &k = f(n); // not a null pointer constant
  98. } ();
  99. const int m = 0;
  100. [=] {
  101. int &k = f(m); // a null pointer constant
  102. } ();
  103. [=] () -> bool {
  104. int &k = f(m); // a null pointer constant
  105. return &m == 0;
  106. } ();
  107. [m] {
  108. int &k = f(m); // a null pointer constant
  109. } ();
  110. }
  111. }
  112. void PR12248()
  113. {
  114. unsigned int result = 0;
  115. auto l = [&]() { ++result; };
  116. }
  117. namespace ModifyingCapture {
  118. void test() {
  119. int n = 0;
  120. [=] {
  121. n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
  122. };
  123. }
  124. }
  125. namespace VariadicPackExpansion {
  126. template<typename T, typename U> using Fst = T;
  127. template<typename...Ts> bool g(Fst<bool, Ts> ...bools);
  128. template<typename...Ts> bool f(Ts &&...ts) {
  129. return g<Ts...>([&ts] {
  130. if (!ts)
  131. return false;
  132. --ts;
  133. return true;
  134. } () ...);
  135. }
  136. void h() {
  137. int a = 5, b = 2, c = 3;
  138. while (f(a, b, c)) {
  139. }
  140. }
  141. struct sink {
  142. template<typename...Ts> sink(Ts &&...) {}
  143. };
  144. template<typename...Ts> void local_class() {
  145. sink {
  146. [] (Ts t) {
  147. struct S : Ts {
  148. void f(Ts t) {
  149. Ts &that = *this;
  150. that = t;
  151. }
  152. Ts g() { return *this; };
  153. };
  154. S s;
  155. s.f(t);
  156. return s;
  157. } (Ts()).g() ...
  158. };
  159. };
  160. struct X {}; struct Y {};
  161. template void local_class<X, Y>();
  162. template<typename...Ts> void nested(Ts ...ts) {
  163. f(
  164. // Each expansion of this lambda implicitly captures all of 'ts', because
  165. // the inner lambda also expands 'ts'.
  166. [&] {
  167. return ts + [&] { return f(ts...); } ();
  168. } () ...
  169. );
  170. }
  171. template void nested(int, int, int);
  172. template<typename...Ts> void nested2(Ts ...ts) { // expected-note 2{{here}}
  173. // Capture all 'ts', use only one.
  174. f([&ts...] { return ts; } ()...);
  175. // Capture each 'ts', use it.
  176. f([&ts] { return ts; } ()...);
  177. // Capture all 'ts', use all of them.
  178. f([&ts...] { return (int)f(ts...); } ());
  179. // Capture each 'ts', use all of them. Ill-formed. In more detail:
  180. //
  181. // We instantiate two lambdas here; the first captures ts$0, the second
  182. // captures ts$1. Both of them reference both ts parameters, so both are
  183. // ill-formed because ts can't be implicitly captured.
  184. //
  185. // FIXME: This diagnostic does not explain what's happening. We should
  186. // specify which 'ts' we're referring to in its diagnostic name. We should
  187. // also say which slice of the pack expansion is being performed in the
  188. // instantiation backtrace.
  189. f([&ts] { return (int)f(ts...); } ()...); // \
  190. // expected-error 2{{'ts' cannot be implicitly captured}} \
  191. // expected-note 2{{lambda expression begins here}}
  192. }
  193. template void nested2(int); // ok
  194. template void nested2(int, int); // expected-note {{in instantiation of}}
  195. }