1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify -std=c++20 %s
2 // RUN: %clang_cc1 -verify=ref -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
; // expected-note {{division by zero}} \
47 // ref-note {{division by zero}}
50 return f(); // expected-note {{in call to '&f->operator()()'}} \
51 // ref-note {{in call to 'f.operator()()'}}
53 static_assert(div(8, 2) == 4);
54 static_assert(div(8, 0) == 4); // expected-error {{not an integral constant expression}} \
55 // expected-note {{in call to 'div(8, 0)'}} \
56 // ref-error {{not an integral constant expression}} \
57 // ref-note {{in call to 'div(8, 0)'}}
64 constexpr float captureStruct() {
74 static_assert(captureStruct() == 1.0);
77 int constexpr FunCase() {
79 decltype(x
) y
; // type int b/c not odr use
80 // refers to original init-capture
81 auto &z
= x
; // type const int & b/c odr use
82 // refers to lambdas copy of x
84 //z = 10; // Ill-formed
89 constexpr int WC
= FunCase();
92 namespace LambdaParams
{
94 constexpr void callThis(T t
) {
100 auto f
= [&a
]() { ++a
; };
106 static_assert(foo() == 1);
109 namespace StaticInvoker
{
110 constexpr int sv1(int i
) {
111 auto l
= []() { return 12; };
115 static_assert(sv1(12) == 12);
117 constexpr int sv2(int i
) {
118 auto l
= [](int m
, float f
, void *A
) { return m
; };
119 int (*fp
)(int, float, void*) = l
;
120 return fp(i
, 4.0f
, nullptr);
122 static_assert(sv2(12) == 12);
124 constexpr int sv3(int i
) {
125 auto l
= [](int m
, const int &n
) { return m
; };
126 int (*fp
)(int, const int &) = l
;
129 static_assert(sv3(12) == 12);
131 constexpr int sv4(int i
) {
132 auto l
= [](int &m
) { return m
; };
136 static_assert(sv4(12) == 12);
138 constexpr int sv5(int i
) {
139 struct F
{ int a
; float f
; };
140 auto l
= [](int m
, F f
) { return m
; };
141 int (*fp
)(int, F
) = l
;
142 return fp(i
, F
{12, 14.0});
144 static_assert(sv5(12) == 12);
146 constexpr int sv6(int i
) {
148 constexpr F(int a
) : a(a
) {}
151 auto l
= [](int m
) { return F(12); };
157 static_assert(sv6(12) == 12);
160 namespace LambdasAsParams
{
162 constexpr auto call(F f
) {
165 static_assert(call([](){ return 1;}) == 1);
166 static_assert(call([](){ return 2;}) == 2);
169 constexpr unsigned L
= call([](){ return 12;});
170 static_assert(L
== 12);
173 constexpr float heh() {
178 return static_cast<float>(a());
180 static_assert(heh() == 1.0);
183 namespace ThisCapture
{
189 constexpr Foo() : a([this](){ return b
+ 1;}()) {}
191 constexpr int Aplus2() const {
200 static_assert(F
.a
== 33, "");
201 static_assert(F
.Aplus2() == (33 + 2), "");