xref: /minix3/external/bsd/llvm/dist/clang/test/Parser/cxx0x-lambda-expressions.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc enum E { e };
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc class C {
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc   int f() {
8*f4a2713aSLionel Sambuc     int foo, bar;
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc     []; // expected-error {{expected body of lambda expression}}
11*f4a2713aSLionel Sambuc     [+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
12*f4a2713aSLionel Sambuc     [foo+] {}; // expected-error {{expected ',' or ']' in lambda capture list}}
13*f4a2713aSLionel Sambuc     [foo,&this] {}; // expected-error {{'this' cannot be captured by reference}}
14*f4a2713aSLionel Sambuc     [&this] {}; // expected-error {{'this' cannot be captured by reference}}
15*f4a2713aSLionel Sambuc     [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
16*f4a2713aSLionel Sambuc     [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
17*f4a2713aSLionel Sambuc     [] {};
18*f4a2713aSLionel Sambuc     [=] (int i) {};
19*f4a2713aSLionel Sambuc     [&] (int) mutable -> void {};
20*f4a2713aSLionel Sambuc     [foo,bar] () { return 3; };
21*f4a2713aSLionel Sambuc     [=,&foo] () {};
22*f4a2713aSLionel Sambuc     [&,foo] () {};
23*f4a2713aSLionel Sambuc     [this] () {};
24*f4a2713aSLionel Sambuc     [] () -> class C { return C(); };
25*f4a2713aSLionel Sambuc     [] () -> enum E { return e; };
26*f4a2713aSLionel Sambuc 
27*f4a2713aSLionel Sambuc     [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}}
28*f4a2713aSLionel Sambuc     [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}}
29*f4a2713aSLionel Sambuc     [](int) -> {}; // PR13652 expected-error {{expected a type}}
30*f4a2713aSLionel Sambuc     return 1;
31*f4a2713aSLionel Sambuc   }
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc   void designator_or_lambda() {
34*f4a2713aSLionel Sambuc     typedef int T;
35*f4a2713aSLionel Sambuc     const int b = 0;
36*f4a2713aSLionel Sambuc     const int c = 1;
37*f4a2713aSLionel Sambuc     int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '<lambda}}
38*f4a2713aSLionel Sambuc     int a2[1] = {[b] = 1 };
39*f4a2713aSLionel Sambuc     int a3[1] = {[b,c] = 1 }; // expected-error{{expected body of lambda expression}}
40*f4a2713aSLionel Sambuc     int a4[1] = {[&b] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}}
41*f4a2713aSLionel Sambuc     int a5[3] = { []{return 0;}() };
42*f4a2713aSLionel Sambuc     int a6[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}}
43*f4a2713aSLionel Sambuc   }
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc   void delete_lambda(int *p) {
46*f4a2713aSLionel Sambuc     delete [] p;
47*f4a2713aSLionel Sambuc     delete [] (int*) { new int }; // ok, compound-literal, not lambda
48*f4a2713aSLionel Sambuc     delete [] { return new int; } (); // expected-error{{expected expression}}
49*f4a2713aSLionel Sambuc     delete [&] { return new int; } (); // ok, lambda
50*f4a2713aSLionel Sambuc   }
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc   // We support init-captures in C++11 as an extension.
53*f4a2713aSLionel Sambuc   int z;
54*f4a2713aSLionel Sambuc   void init_capture() {
55*f4a2713aSLionel Sambuc     [n(0)] () mutable -> int { return ++n; }; // expected-warning{{extension}}
56*f4a2713aSLionel Sambuc     [n{0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}}
57*f4a2713aSLionel Sambuc     [n = 0] { return ++n; }; // expected-error {{captured by copy in a non-mutable}} expected-warning{{extension}}
58*f4a2713aSLionel Sambuc     [n = {0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}}
59*f4a2713aSLionel Sambuc     [a([&b = z]{})](){}; // expected-warning 2{{extension}}
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc     int x = 4;
62*f4a2713aSLionel Sambuc     auto y = [&r = x, x = x + 1]() -> int { // expected-warning 2{{extension}}
63*f4a2713aSLionel Sambuc       r += 2;
64*f4a2713aSLionel Sambuc       return x + 2;
65*f4a2713aSLionel Sambuc     } ();
66*f4a2713aSLionel Sambuc   }
67*f4a2713aSLionel Sambuc };
68