xref: /llvm-project/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p13.cpp (revision 742970920b7a7fc2fe1cb6bca6fb04f03ab7d5d9)
1 // RUN: %clang_cc1 -std=c++11 %s -Wunused -Wno-unused-lambda-capture -Wno-c++14-extensions -verify
2 // RUN: %clang_cc1 -std=c++17 %s -Wunused -Wno-unused-lambda-capture -Wno-c++14-extensions -verify
3 
4 
5 const int global = 0;
6 
f2()7 void f2() {
8   int i = 1;
9   void g1(int = ([i]{ return i; })()); // expected-error{{lambda expression in default argument cannot capture any entity}}
10   void g2(int = ([i]{ return 0; })()); // expected-error{{lambda expression in default argument cannot capture any entity}}
11   void g3(int = ([=]{ return i; })()); // expected-error{{lambda expression in default argument cannot capture any entity}}
12   void g4(int = ([=]{ return 0; })());
13   void g5(int = ([]{ return sizeof i; })());
14   void g6(int = ([x=1, y = global, &z = global]{ return x; })());
15   void g7(int = ([x=i, &y=i]{ return x; })()); // expected-error 2{{default argument references local variable 'i' of enclosing function}}
16 }
17 
18 #if __cplusplus >= 201703L
19 int global_array[] = { 1, 2 };
20 auto [ga, gb] = global_array;
21 
structured_bindings()22 void structured_bindings() {
23   int array[] = { 1, 2 };
24   auto [a, b] = array;
25   void func(int c = [x = a, &xref = a, y = ga, &yref = ga] { return x; }()); // expected-error 2{{default argument references local variable 'a' of enclosing function}}
26 }
27 #endif
28 
29 namespace lambda_in_default_args {
__anon92de6f910902() 30   int f(int = [] () -> int { int n; return ++n; } ());
__anon92de6f910a02() 31   template<typename T> T g(T = [] () -> T { T n; return ++n; } ());
32   int k = f() + g<int>();
33 }
34