1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both -std=c++20 %s
2 // RUN: %clang_cc1 -verify=ref,both -std=c++20 %s
5 constexpr int f
= [c
= a
]() { return c
; }();
19 static_assert(inc() == 12);
21 constexpr int add(int a
, int b
) {
22 auto doIt
= [a
, b
](int c
) {
28 static_assert(add(4, 5) == 11);
31 constexpr int add2(int a
, int b
) {
32 auto doIt
= [a
, b
](int c
) {
33 auto bar
= [a
]() { return a
; };
34 auto bar2
= [b
]() { return b
; };
36 return bar() + bar2() + c
;
41 static_assert(add2(4, 5) == 11);
44 constexpr int div(int a
, int b
) {
46 return a
/ b
; // both-note {{division by zero}}
49 return f(); // both-note {{in call to 'f.operator()()'}}
51 static_assert(div(8, 2) == 4);
52 static_assert(div(8, 0) == 4); // both-error {{not an integral constant expression}} \
53 // both-note {{in call to 'div(8, 0)'}}
60 constexpr float captureStruct() {
70 static_assert(captureStruct() == 1.0);
73 int constexpr FunCase() {
75 decltype(x
) y
; // type int b/c not odr use
76 // refers to original init-capture
77 auto &z
= x
; // type const int & b/c odr use
78 // refers to lambdas copy of x
80 //z = 10; // Ill-formed
85 constexpr int WC
= FunCase();
88 namespace LambdaParams
{
90 constexpr void callThis(T t
) {
96 auto f
= [&a
]() { ++a
; };
102 static_assert(foo() == 1);
105 namespace StaticInvoker
{
106 constexpr int sv1(int i
) {
107 auto l
= []() { return 12; };
111 static_assert(sv1(12) == 12);
113 constexpr int sv2(int i
) {
114 auto l
= [](int m
, float f
, void *A
) { return m
; };
115 int (*fp
)(int, float, void*) = l
;
116 return fp(i
, 4.0f
, nullptr);
118 static_assert(sv2(12) == 12);
120 constexpr int sv3(int i
) {
121 auto l
= [](int m
, const int &n
) { return m
; };
122 int (*fp
)(int, const int &) = l
;
125 static_assert(sv3(12) == 12);
127 constexpr int sv4(int i
) {
128 auto l
= [](int &m
) { return m
; };
132 static_assert(sv4(12) == 12);
134 constexpr int sv5(int i
) {
135 struct F
{ int a
; float f
; };
136 auto l
= [](int m
, F f
) { return m
; };
137 int (*fp
)(int, F
) = l
;
138 return fp(i
, F
{12, 14.0});
140 static_assert(sv5(12) == 12);
142 constexpr int sv6(int i
) {
144 constexpr F(int a
) : a(a
) {}
147 auto l
= [](int m
) { return F(12); };
153 static_assert(sv6(12) == 12);
156 /// A generic lambda.
157 auto GL
= [](auto a
) { return a
; };
158 constexpr char (*fp2
)(char) = GL
;
159 static_assert(fp2('3') == '3', "");
164 auto GL2
= [](auto a
) { return GLS
{a
}; };
165 constexpr GLS (*fp3
)(char) = GL2
;
166 static_assert(fp3('3').a
== '3', "");
169 namespace LambdasAsParams
{
171 constexpr auto call(F f
) {
174 static_assert(call([](){ return 1;}) == 1);
175 static_assert(call([](){ return 2;}) == 2);
178 constexpr unsigned L
= call([](){ return 12;});
179 static_assert(L
== 12);
182 constexpr float heh() {
187 return static_cast<float>(a());
189 static_assert(heh() == 1.0);
192 namespace ThisCapture
{
198 constexpr Foo() : a([this](){ return b
+ 1;}()) {}
200 constexpr int Aplus2() const {
209 static_assert(F
.a
== 33, "");
210 static_assert(F
.Aplus2() == (33 + 2), "");
214 template <auto A
= [](auto x
){}>
216 static constexpr auto B
= A
;
225 namespace LambdaToAPValue
{
227 constexpr auto f
= []() constexpr {
231 constexpr auto g
= [f
]() constexpr {
234 static_assert(g() == f(), "");
238 namespace ns2_capture_this_byval
{
241 constexpr S(int s
) : s
{s
} { }
242 constexpr auto f(S o
) {
243 return [*this,o
] (auto a
) { return s
+ o
.s
+ a
.s
; };
247 constexpr auto L
= S
{5}.f(S
{10});
248 static_assert(L(S
{100}) == 115, "");
249 } // end test_captures_1::ns2_capture_this_byval
251 namespace CaptureDefaults
{
256 constexpr auto f
= [x
= S
{10}]() {
259 static_assert(f() == 10, "");
261 constexpr auto f2
= [x
= 3]() {
264 static_assert(f2() == 3, "");
267 constexpr auto t4
= ([x
=42]() consteval
{ return x
; }());
268 static_assert(t4
== 42, "");
270 namespace InvalidCapture
{
275 int n
= -1; // both-note {{declared here}}
277 int arr
[n
]; // both-warning {{variable length arrays in C++ are a Clang extension}} \
278 both
-note
{{read of non
-const variable
'n' is
not allowed in a constant expression
}}
285 return [=]() constexpr { return Capture
; }();
287 static_assert(fn() == 42, "");