1 // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 -Wno-c99-designator %s
2 // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++20 -Wno-c99-designator %s
3 // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++23 -Wno-c99-designator %s
7 constexpr int id(int n
) { return n
; }
14 []; // expected-error {{expected body of lambda expression}}
15 [+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
16 [foo
+] {}; // expected-error {{expected ',' or ']' in lambda capture list}}
17 [foo
,&this] {}; // expected-error {{'this' cannot be captured by reference}}
18 [&this] {}; // expected-error {{'this' cannot be captured by reference}}
19 [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
20 [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
23 [&] (int) mutable -> void {};
24 [foo
,bar
] () { return 3; };
28 [] () -> class C
{ return C(); };
29 [] () -> enum E
{ return e
; };
31 [] -> int { return 0; };
32 [] mutable -> int { return 0; };
33 #if __cplusplus <= 202002L
34 // expected-warning@-3 {{lambda without a parameter clause is a C++23 extension}}
35 // expected-warning@-3 {{is a C++23 extension}}
37 [](int) -> {}; // PR13652 expected-error {{expected a type}}
41 void designator_or_lambda() {
46 int a1
[1] = {[b
] (T()) {}}; // expected-error{{no viable conversion from '(lambda}}
47 int a2
[1] = {[b
] = 1 };
48 int a3
[1] = {[b
,c
] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}}
49 int a4
[1] = {[&b
] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}}
50 int a5
[3] = { []{return 0;}() };
51 int a6
[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}}
52 int a7
[1] = {[d(0)] { return d
; } ()};
53 int a8
[1] = {[d
= 0] { return d
; } ()};
54 int a10
[1] = {[id(0)] { return id
; } ()};
55 #if __cplusplus <= 201103L
56 // expected-warning@-4{{extension}}
57 // expected-warning@-4{{extension}}
58 // expected-warning@-4{{extension}}
60 int a9
[1] = {[d
= 0] = 1}; // expected-error{{is not an integral constant expression}}
61 #if __cplusplus >= 201402L
62 // expected-note@-2{{constant expression cannot modify an object that is visible outside that expression}}
64 int a11
[1] = {[id(0)] = 1};
67 void delete_lambda(int *p
) {
69 delete [] (int*) { new int }; // ok, compound-literal, not lambda
70 delete [] { return new int; } (); // expected-error {{'[]' after delete interpreted as 'delete[]'}}
71 delete [&] { return new int; } (); // ok, lambda
73 delete []() { return new int; }(); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
74 delete [](E Enum
) { return new int((int)Enum
); }(e
); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
75 #if __cplusplus > 201703L
76 delete []<int = 0>() { return new int; }(); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
80 // We support init-captures in C++11 as an extension.
83 [n(0)] () mutable -> int { return ++n
; };
86 [n
= 0] { return ++n
; }; // expected-error {{captured by copy in a non-mutable}}
87 [n
= {0}] { return; }; // expected-error {{<initializer_list>}}
88 #if __cplusplus <= 201103L
89 // expected-warning@-6{{extension}}
90 // expected-warning@-6{{extension}}
91 // expected-warning@-6{{extension}}
92 // expected-warning@-7{{extension}}
93 // expected-warning@-7{{extension}}
94 // expected-warning@-7{{extension}}
98 auto y
= [&r
= x
, x
= x
+ 1]() -> int {
99 #if __cplusplus <= 201103L
100 // expected-warning@-2{{extension}}
101 // expected-warning@-3{{extension}}
109 [] __attribute__((noreturn
)){};
110 #if __cplusplus <= 202002L
111 // expected-warning@-2 {{is a C++23 extension}}
114 mutable {}; // expected-error {{expected body of lambda expression}}
117 []() [[]] -> void {};
118 []() mutable [[]] -> void {};
119 []() mutable noexcept
[[]] -> void {};
121 // Testing GNU-style attributes on lambdas -- the attribute is specified
122 // before the mutable specifier instead of after (unlike C++11).
123 []() __attribute__((noreturn
)) mutable { while(1); };
125 __attribute__((noreturn
)) { while(1); }; // expected-error {{expected body of lambda expression}}
127 // Testing support for P2173 on adding attributes to the declaration
128 // rather than the type.
130 #if __cplusplus <= 202002L
131 // expected-warning@-2 {{an attribute specifier sequence in this position is a C++23 extension}}
133 #if __cplusplus > 201703L
134 []<typename
>[[]](){};
135 #if __cplusplus <= 202002L
136 // expected-warning@-2 {{an attribute specifier sequence in this position is a C++23 extension}}
140 #if __cplusplus <= 202002L
141 // expected-warning@-2 {{an attribute specifier sequence in this position is a C++23 extension}}
145 void missing_parens() {
148 #if __cplusplus <= 202002L
149 // expected-warning@-3 {{is a C++23 extension}}
150 // expected-warning@-3 {{is a C++23 extension}}
157 [](int) -> {}; // expected-error {{expected a type}}
160 template void PR22122
<int>();
164 template <class F
> A(F
&&) {}
168 void mf() { A
{[*this]{}}; }
169 #if __cplusplus < 201703L
170 // expected-warning@-2 {{C++17 extension}}
176 template <typename T
>
177 void m (T x
=[0); // expected-error{{expected variable name or 'this' in lambda capture list}}
181 template <typename T
>
182 void m_fn1(T x
= 0[0); // expected-error{{expected ']'}} expected-note{{to match this '['}}