xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/cxx1y-init-captures.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -std=c++1y %s -verify -emit-llvm-only
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc namespace variadic_expansion {
f(int &,char &)4*0a6a1f1dSLionel Sambuc   int f(int &, char &) { return 0; }
fv(Ts...ts)5*0a6a1f1dSLionel Sambuc   template<class ... Ts> char fv(Ts ... ts) { return 0; }
6*0a6a1f1dSLionel Sambuc   // FIXME: why do we get 2 error messages
g(T &...t)7*0a6a1f1dSLionel Sambuc   template <typename ... T> void g(T &... t) { //expected-note3{{declared here}}
8f4a2713aSLionel Sambuc     f([&a(t)]()->decltype(auto) {
9f4a2713aSLionel Sambuc       return a;
10f4a2713aSLionel Sambuc     }() ...);
11*0a6a1f1dSLionel Sambuc 
12*0a6a1f1dSLionel Sambuc     auto L = [x = f([&a(t)]()->decltype(auto) { return a; }()...)]() { return x; };
13*0a6a1f1dSLionel Sambuc     const int y = 10;
14*0a6a1f1dSLionel Sambuc     auto M = [x = y,
15*0a6a1f1dSLionel Sambuc                 &z = y](T& ... t) { };
16*0a6a1f1dSLionel Sambuc     auto N = [x = y,
17*0a6a1f1dSLionel Sambuc                 &z = y, n = f(t...),
18*0a6a1f1dSLionel Sambuc                 o = f([&a(t)](T& ... t)->decltype(auto) { return a; }(t...)...), t...](T& ... s) {
19*0a6a1f1dSLionel Sambuc                   fv([&a(t)]()->decltype(auto) {
20*0a6a1f1dSLionel Sambuc                     return a;
21*0a6a1f1dSLionel Sambuc                   }() ...);
22*0a6a1f1dSLionel Sambuc                 };
23*0a6a1f1dSLionel Sambuc     auto N2 = [x = y,                     //expected-note3{{begins here}}
24*0a6a1f1dSLionel Sambuc                 &z = y, n = f(t...),
25*0a6a1f1dSLionel Sambuc                 o = f([&a(t)](T& ... t)->decltype(auto) { return a; }(t...)...)](T& ... s) {
26*0a6a1f1dSLionel Sambuc                   fv([&a(t)]()->decltype(auto) { //expected-error 3{{captured}}
27*0a6a1f1dSLionel Sambuc                     return a;
28*0a6a1f1dSLionel Sambuc                   }() ...);
29*0a6a1f1dSLionel Sambuc                 };
30*0a6a1f1dSLionel Sambuc 
31f4a2713aSLionel Sambuc   }
32f4a2713aSLionel Sambuc 
h(int i,char c)33*0a6a1f1dSLionel Sambuc   void h(int i, char c) { g(i, c); } //expected-note{{in instantiation}}
34*0a6a1f1dSLionel Sambuc }
35*0a6a1f1dSLionel Sambuc 
36*0a6a1f1dSLionel Sambuc namespace odr_use_within_init_capture {
37*0a6a1f1dSLionel Sambuc 
test()38*0a6a1f1dSLionel Sambuc int test() {
39*0a6a1f1dSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc   { // no captures
41*0a6a1f1dSLionel Sambuc     const int x = 10;
42*0a6a1f1dSLionel Sambuc     auto L = [z = x + 2](int a) {
43*0a6a1f1dSLionel Sambuc       auto M = [y = x - 2](char b) {
44*0a6a1f1dSLionel Sambuc         return y;
45*0a6a1f1dSLionel Sambuc       };
46*0a6a1f1dSLionel Sambuc       return M;
47*0a6a1f1dSLionel Sambuc     };
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc   }
50*0a6a1f1dSLionel Sambuc   { // should not capture
51*0a6a1f1dSLionel Sambuc     const int x = 10;
52*0a6a1f1dSLionel Sambuc     auto L = [&z = x](int a) {
53*0a6a1f1dSLionel Sambuc       return a;;
54*0a6a1f1dSLionel Sambuc     };
55*0a6a1f1dSLionel Sambuc 
56*0a6a1f1dSLionel Sambuc   }
57*0a6a1f1dSLionel Sambuc   {
58*0a6a1f1dSLionel Sambuc     const int x = 10;
59*0a6a1f1dSLionel Sambuc     auto L = [k = x](char a) { //expected-note {{declared}}
60*0a6a1f1dSLionel Sambuc       return [](int b) { //expected-note {{begins}}
61*0a6a1f1dSLionel Sambuc         return [j = k](int c) { //expected-error {{cannot be implicitly captured}}
62*0a6a1f1dSLionel Sambuc           return c;
63*0a6a1f1dSLionel Sambuc         };
64*0a6a1f1dSLionel Sambuc       };
65*0a6a1f1dSLionel Sambuc     };
66*0a6a1f1dSLionel Sambuc   }
67*0a6a1f1dSLionel Sambuc   {
68*0a6a1f1dSLionel Sambuc     const int x = 10;
69*0a6a1f1dSLionel Sambuc     auto L = [k = x](char a) {
70*0a6a1f1dSLionel Sambuc       return [=](int b) {
71*0a6a1f1dSLionel Sambuc         return [j = k](int c) {
72*0a6a1f1dSLionel Sambuc           return c;
73*0a6a1f1dSLionel Sambuc         };
74*0a6a1f1dSLionel Sambuc       };
75*0a6a1f1dSLionel Sambuc     };
76*0a6a1f1dSLionel Sambuc   }
77*0a6a1f1dSLionel Sambuc   {
78*0a6a1f1dSLionel Sambuc     const int x = 10;
79*0a6a1f1dSLionel Sambuc     auto L = [k = x](char a) {
80*0a6a1f1dSLionel Sambuc       return [k](int b) {
81*0a6a1f1dSLionel Sambuc         return [j = k](int c) {
82*0a6a1f1dSLionel Sambuc           return c;
83*0a6a1f1dSLionel Sambuc         };
84*0a6a1f1dSLionel Sambuc       };
85*0a6a1f1dSLionel Sambuc     };
86*0a6a1f1dSLionel Sambuc   }
87*0a6a1f1dSLionel Sambuc 
88*0a6a1f1dSLionel Sambuc   return 0;
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc 
91*0a6a1f1dSLionel Sambuc int run = test();
92*0a6a1f1dSLionel Sambuc 
93*0a6a1f1dSLionel Sambuc }
94*0a6a1f1dSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc namespace odr_use_within_init_capture_template {
96*0a6a1f1dSLionel Sambuc 
97*0a6a1f1dSLionel Sambuc template<class T = int>
test(T t=T{})98*0a6a1f1dSLionel Sambuc int test(T t = T{}) {
99*0a6a1f1dSLionel Sambuc 
100*0a6a1f1dSLionel Sambuc   { // no captures
101*0a6a1f1dSLionel Sambuc     const T x = 10;
__anon47b09c901102(char a) 102*0a6a1f1dSLionel Sambuc     auto L = [z = x](char a) {
103*0a6a1f1dSLionel Sambuc       auto M = [y = x](T b) {
104*0a6a1f1dSLionel Sambuc         return y;
105*0a6a1f1dSLionel Sambuc       };
106*0a6a1f1dSLionel Sambuc       return M;
107*0a6a1f1dSLionel Sambuc     };
108*0a6a1f1dSLionel Sambuc 
109*0a6a1f1dSLionel Sambuc   }
110*0a6a1f1dSLionel Sambuc   { // should not capture
111*0a6a1f1dSLionel Sambuc     const T x = 10;
__anon47b09c901302(T a) 112*0a6a1f1dSLionel Sambuc     auto L = [&z = x](T a) {
113*0a6a1f1dSLionel Sambuc       return a;;
114*0a6a1f1dSLionel Sambuc     };
115*0a6a1f1dSLionel Sambuc 
116*0a6a1f1dSLionel Sambuc   }
117*0a6a1f1dSLionel Sambuc   { // will need to capture x in outer lambda
118*0a6a1f1dSLionel Sambuc     const T x = 10; //expected-note {{declared}}
__anon47b09c901402(char a) 119*0a6a1f1dSLionel Sambuc     auto L = [z = x](char a) { //expected-note {{begins}}
120*0a6a1f1dSLionel Sambuc       auto M = [&y = x](T b) { //expected-error {{cannot be implicitly captured}}
121*0a6a1f1dSLionel Sambuc         return y;
122*0a6a1f1dSLionel Sambuc       };
123*0a6a1f1dSLionel Sambuc       return M;
124*0a6a1f1dSLionel Sambuc     };
125*0a6a1f1dSLionel Sambuc 
126*0a6a1f1dSLionel Sambuc   }
127*0a6a1f1dSLionel Sambuc   { // will need to capture x in outer lambda
128*0a6a1f1dSLionel Sambuc     const T x = 10;
__anon47b09c901602(char a) 129*0a6a1f1dSLionel Sambuc     auto L = [=,z = x](char a) {
130*0a6a1f1dSLionel Sambuc       auto M = [&y = x](T b) {
131*0a6a1f1dSLionel Sambuc         return y;
132*0a6a1f1dSLionel Sambuc       };
133*0a6a1f1dSLionel Sambuc       return M;
134*0a6a1f1dSLionel Sambuc     };
135*0a6a1f1dSLionel Sambuc 
136*0a6a1f1dSLionel Sambuc   }
137*0a6a1f1dSLionel Sambuc   { // will need to capture x in outer lambda
138*0a6a1f1dSLionel Sambuc     const T x = 10;
__anon47b09c901802(char a) 139*0a6a1f1dSLionel Sambuc     auto L = [x, z = x](char a) {
140*0a6a1f1dSLionel Sambuc       auto M = [&y = x](T b) {
141*0a6a1f1dSLionel Sambuc         return y;
142*0a6a1f1dSLionel Sambuc       };
143*0a6a1f1dSLionel Sambuc       return M;
144*0a6a1f1dSLionel Sambuc     };
145*0a6a1f1dSLionel Sambuc   }
146*0a6a1f1dSLionel Sambuc   { // will need to capture x in outer lambda
147*0a6a1f1dSLionel Sambuc     const int x = 10; //expected-note 2{{declared}}
__anon47b09c901a02(char a) 148*0a6a1f1dSLionel Sambuc     auto L = [z = x](char a) { //expected-note 2{{begins}}
149*0a6a1f1dSLionel Sambuc       auto M = [&y = x](T b) { //expected-error 2{{cannot be implicitly captured}}
150*0a6a1f1dSLionel Sambuc         return y;
151*0a6a1f1dSLionel Sambuc       };
152*0a6a1f1dSLionel Sambuc       return M;
153*0a6a1f1dSLionel Sambuc     };
154*0a6a1f1dSLionel Sambuc   }
155*0a6a1f1dSLionel Sambuc   {
156*0a6a1f1dSLionel Sambuc     // no captures
157*0a6a1f1dSLionel Sambuc     const T x = 10;
158*0a6a1f1dSLionel Sambuc     auto L = [z =
__anon47b09c901c02(char a) 159*0a6a1f1dSLionel Sambuc                   [z = x, &y = x](char a) { return z + y; }('a')](char a)
__anon47b09c901d02(char a) 160*0a6a1f1dSLionel Sambuc       { return z; };
161*0a6a1f1dSLionel Sambuc 
162*0a6a1f1dSLionel Sambuc   }
163*0a6a1f1dSLionel Sambuc 
164*0a6a1f1dSLionel Sambuc   return 0;
165*0a6a1f1dSLionel Sambuc }
166*0a6a1f1dSLionel Sambuc 
167*0a6a1f1dSLionel Sambuc int run = test(); //expected-note {{instantiation}}
168*0a6a1f1dSLionel Sambuc 
169f4a2713aSLionel Sambuc }