[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / lambda-expressions-gh56071.cpp
blobd45d2d9b2e40976b0f8afa74ab455ca894f87212
1 // RUN: %clang_cc1 -fsyntax-only -verify %s -Wno-unused-value -std=c++17
2 // expected-no-diagnostics
3 namespace test1 {
5 template <int num> int return_num() { return num; }
7 template <typename lambda> struct lambda_wrapper {
8 lambda &outer_lambda;
9 lambda_wrapper(lambda& outer_lambda) : outer_lambda(outer_lambda) {}
10 template <typename T> auto operator+(T t) { outer_lambda(t); return 1; }
13 template <int... nums, typename lambda>
14 void bw(lambda& outer_lambda) {
15 (lambda_wrapper(outer_lambda) + ... + return_num<nums>());
18 template <typename lambda> auto check_return_type(lambda inner_lambda) {
19 using inner_lambda_return_type = decltype(inner_lambda(5));
22 void cs() {
23 auto outer_lambda = [](auto param) {
24 auto inner_lambda = [](auto o) -> decltype(param) {};
25 check_return_type(inner_lambda);
27 bw<1,2>(outer_lambda);
30 } // namespace test1
32 namespace test2 {
34 template <typename lambda>
35 auto run_lambda_with_zero(lambda l) {
36 l(0);
38 template <typename ... Ts, typename lambda>
39 void run_lambda_once_per_type(lambda l) {
40 ((Ts{}, run_lambda_with_zero(l)), ...);
42 template <typename> void inner_function() {
43 char c;
44 [](auto param) -> decltype(c) { return param; }(0);
46 void run() {
47 auto x = [](auto) -> void { inner_function<int>(); };
48 run_lambda_once_per_type<int>(x);
51 } // namespace test2