[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / cxx1y-generic-lambdas.cpp
blob463e077ce934cee840a08414b3cf7ac9f25d7753
1 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -emit-llvm-only %s
2 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fdelayed-template-parsing %s -DDELAYED_TEMPLATE_PARSING
3 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fms-extensions %s -DMS_EXTENSIONS
4 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fdelayed-template-parsing -fms-extensions %s -DMS_EXTENSIONS -DDELAYED_TEMPLATE_PARSING
5 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -triple i386-windows-pc -emit-llvm-only %s
6 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -triple i386-windows-pc -fdelayed-template-parsing %s -DDELAYED_TEMPLATE_PARSING
7 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -triple i386-windows-pc -fms-extensions %s -DMS_EXTENSIONS
8 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -triple i386-windows-pc -fdelayed-template-parsing -fms-extensions %s -DMS_EXTENSIONS -DDELAYED_TEMPLATE_PARSING
10 template<class F, class ...Rest> struct first_impl { typedef F type; };
11 template<class ...Args> using first = typename first_impl<Args...>::type;
13 namespace simple_explicit_capture {
14 void test() {
15 int i;
16 auto L = [i](auto a) { return i + a; };
17 L(3.14);
21 namespace explicit_call {
22 int test() {
23 auto L = [](auto a) { return a; };
24 L.operator()(3);
25 L.operator()<char>(3.14); //expected-warning{{implicit conversion}}
26 return 0;
28 } //end ns
30 namespace test_conversion_to_fptr_2 {
32 template<class T> struct X {
34 T (*fp)(T) = [](auto a) { return a; };
38 X<int> xi;
40 template<class T>
41 void fooT(T t, T (*fp)(T) = [](auto a) { return a; }) {
42 fp(t);
45 int test() {
47 auto L = [](auto a) { return a; };
48 int (*fp)(int) = L;
49 fp(5);
50 L(3);
51 char (*fc)(char) = L;
52 fc('b');
53 L('c');
54 double (*fd)(double) = L;
55 fd(3.14);
56 fd(6.26);
57 L(4.25);
60 auto L = [](auto a) ->int { return a; }; //expected-note 2{{candidate template ignored}}
61 int (*fp)(int) = L;
62 char (*fc)(char) = L; //expected-error{{no viable conversion}}
63 double (*fd)(double) = L; //expected-error{{no viable conversion}}
66 int x = 5;
67 auto L = [=](auto b, char c = 'x') {
68 int i = x;
69 return [](auto a) ->decltype(a) { return a; };
71 int (*fp)(int) = L(8);
72 fp(5);
73 L(3);
74 char (*fc)(char) = L('a');
75 fc('b');
76 L('c');
77 double (*fd)(double) = L(3.14);
78 fd(3.14);
79 fd(6.26);
83 auto L = [=](auto b) {
84 return [](auto a) ->decltype(b)* { return (decltype(b)*)0; };
86 int* (*fp)(int) = L(8);
87 fp(5);
88 L(3);
89 char* (*fc)(char) = L('a');
90 fc('b');
91 L('c');
92 double* (*fd)(double) = L(3.14);
93 fd(3.14);
94 fd(6.26);
97 auto L = [=](auto b) {
98 return [](auto a) ->decltype(b)* { return (decltype(b)*)0; }; //expected-note{{candidate template ignored}}
100 char* (*fp)(int) = L('8');
101 fp(5);
102 char* (*fc)(char) = L('a');
103 fc('b');
104 double* (*fi)(int) = L(3.14);
105 fi(5);
106 int* (*fi2)(int) = L(3.14); //expected-error{{no viable conversion}}
110 auto L = [=](auto b) {
111 return [](auto a) {
112 return [=](auto c) {
113 return [](auto d) ->decltype(a + b + c + d) { return d; };
117 int (*fp)(int) = L('8')(3)(short{});
118 double (*fs)(char) = L(3.14)(short{})('4');
121 fooT(3);
122 fooT('a');
123 fooT(3.14);
124 fooT("abcdefg");
125 return 0;
127 int run2 = test();
132 namespace test_conversion_to_fptr {
134 void f1(int (*)(int)) { }
135 void f2(char (*)(int)) { } // expected-note{{candidate}}
136 void g(int (*)(int)) { } // #1 expected-note{{candidate}}
137 void g(char (*)(char)) { } // #2 expected-note{{candidate}}
138 void h(int (*)(int)) { } // #3
139 void h(char (*)(int)) { } // #4
141 int test() {
143 auto glambda = [](auto a) { return a; };
144 glambda(1);
145 f1(glambda); // OK
146 f2(glambda); // expected-error{{no matching function}}
147 g(glambda); // expected-error{{call to 'g' is ambiguous}}
148 h(glambda); // OK: calls #3 since it is convertible from ID
150 int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
155 auto L = [](auto a) { return a; };
156 int (*fp)(int) = L;
157 fp(5);
158 L(3);
159 char (*fc)(char) = L;
160 fc('b');
161 L('c');
162 double (*fd)(double) = L;
163 fd(3.14);
164 fd(6.26);
165 L(4.25);
168 auto L = [](auto a) ->int { return a; }; //expected-note 2{{candidate template ignored}}
169 int (*fp)(int) = L;
170 char (*fc)(char) = L; //expected-error{{no viable conversion}}
171 double (*fd)(double) = L; //expected-error{{no viable conversion}}
174 int* (*fp)(int*) = [](auto *a) -> auto* { return a; };
175 fp(0);
179 namespace more_converion_to_ptr_to_function_tests {
182 int test() {
184 int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
185 int (*fp2)(int) = [](auto b) -> int { return b; };
186 int (*fp3)(char) = [](auto c) -> int { return c; };
187 char (*fp4)(int) = [](auto d) { return d; }; //expected-error{{no viable conversion}}\
188 //expected-note{{candidate function [with d:auto = int]}}
189 char (*fp5)(char) = [](auto e) -> int { return e; }; //expected-error{{no viable conversion}}\
190 //expected-note{{candidate template ignored}}
192 fp2(3);
193 fp3('\n');
194 fp3('a');
195 return 0;
197 } // end test()
199 template<class ... Ts> void vfun(Ts ... ) { }
201 int variadic_test() {
203 int (*fp)(int, char, double) = [](auto ... a) -> int { vfun(a...); return 4; };
204 fp(3, '4', 3.14);
206 int (*fp2)(int, char, double) = [](auto ... a) { vfun(a...); return 4; };
207 fp(3, '4', 3.14);
208 return 2;
211 } // end ns
213 namespace conversion_operator {
214 void test() {
215 auto L = [](auto a) -> int { return a; }; // expected-error {{cannot initialize}}
216 int (*fp)(int) = L;
217 int (&fp2)(int) = [](auto a) { return a; }; // expected-error{{non-const lvalue}}
218 int (&&fp3)(int) = [](auto a) { return a; };
219 // expected-error@-1 {{no viable conversion}}
220 // expected-note-re@-2 {{candidate template ignored: could not match 'auto (*)(type-parameter-0-0){{.*}}' against 'int (int)'}}
222 using F = int(int);
223 using G = int(void*);
224 L.operator F*();
225 L.operator G*(); // expected-note-re {{instantiation of function template specialization '{{.*}}::operator()<void *>'}}
227 // Here, the conversion function is named 'operator auto (*)(int)', and
228 // there is no way to write that name in valid C++.
229 auto M = [](auto a) -> auto { return a; };
230 M.operator F*(); // expected-error {{no member named 'operator int (*)(int)'}}
235 namespace return_type_deduction_ok {
236 auto l = [](auto a) ->auto { return a; }(2);
237 auto l2 = [](auto a) ->decltype(auto) { return a; }(2);
238 auto l3 = [](auto a) { return a; }(2);
242 namespace generic_lambda_as_default_argument_ok {
243 void test(int i = [](auto a)->int { return a; }(3)) {
247 namespace nested_non_capturing_lambda_tests {
248 template<class ... Ts> void print(Ts ...) { }
249 int test() {
251 auto L = [](auto a) {
252 return [](auto b) {
253 return b;
256 auto M = L(3);
257 M(4.15);
260 int i = 10; //expected-note 3{{declared here}}
261 auto L = [](auto a) {
262 return [](auto b) { //expected-note 3{{begins here}} expected-note 6 {{capture 'i' by}} expected-note 6 {{default capture by}}
263 i = b; //expected-error 3{{cannot be implicitly captured}}
264 return b;
267 auto M = L(3); //expected-note{{instantiation}}
268 M(4.15); //expected-note{{instantiation}}
271 int i = 10;
272 auto L = [](auto a) {
273 return [](auto b) {
274 b = sizeof(i); //ok
275 return b;
280 auto L = [](auto a) {
281 print("a = ", a, "\n");
282 return [](auto b) ->decltype(a) {
283 print("b = ", b, "\n");
284 return b;
287 auto M = L(3);
288 M(4.15);
292 auto L = [](auto a) ->decltype(a) {
293 print("a = ", a, "\n");
294 return [](auto b) ->decltype(a) {
295 // expected-error@-1 {{no viable conversion}}
296 // expected-note-re@-2 {{candidate template ignored: could not match 'int (*)(type-parameter-0-0){{.*}}' against 'int'}}
297 print("b = ", b, "\n");
298 return b;
301 auto M = L(3); //expected-note{{in instantiation of}}
304 auto L = [](auto a) {
305 print("a = ", a, "\n");
306 return [](auto ... b) ->decltype(a) {
307 print("b = ", b ..., "\n");
308 return 4;
311 auto M = L(3);
312 M(4.15, 3, "fv");
316 auto L = [](auto a) {
317 print("a = ", a, "\n");
318 return [](auto ... b) ->decltype(a) {
319 print("b = ", b ..., "\n");
320 return 4;
323 auto M = L(3);
324 int (*fp)(double, int, const char*) = M;
325 fp(4.15, 3, "fv");
329 auto L = [](auto a) {
330 print("a = ", a, "\n");
331 return [](char b) {
332 return [](auto ... c) ->decltype(b) {
333 print("c = ", c ..., "\n");
334 return 42;
338 L(4);
339 auto M = L(3);
340 M('a');
341 auto N = M('x');
342 N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
343 char (*np)(const char*, int, const char*, double, const char*, int) = N;
344 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
349 auto L = [](auto a) {
350 print("a = ", a, "\n");
351 return [](decltype(a) b) {
352 return [](auto ... c) ->decltype(b) {
353 print("c = ", c ..., "\n");
354 return 42;
358 L('4');
359 auto M = L('3');
360 M('a');
361 auto N = M('x');
362 N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
363 char (*np)(const char*, int, const char*, double, const char*, int) = N;
364 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
369 struct X {
370 static void foo(double d) { }
371 void test() {
372 auto L = [](auto a) {
373 print("a = ", a, "\n");
374 foo(a);
375 return [](decltype(a) b) {
376 foo(b);
377 foo(sizeof(a) + sizeof(b));
378 return [](auto ... c) ->decltype(b) {
379 print("c = ", c ..., "\n");
380 foo(decltype(b){});
381 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
382 return 42;
386 L('4');
387 auto M = L('3');
388 M('a');
389 auto N = M('x');
390 N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
391 char (*np)(const char*, int, const char*, double, const char*, int) = N;
392 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
395 X x;
396 x.test();
398 // Make sure we can escape the function
400 struct X {
401 static void foo(double d) { }
402 auto test() {
403 auto L = [](auto a) {
404 print("a = ", a, "\n");
405 foo(a);
406 return [](decltype(a) b) {
407 foo(b);
408 foo(sizeof(a) + sizeof(b));
409 return [](auto ... c) ->decltype(b) {
410 print("c = ", c ..., "\n");
411 foo(decltype(b){});
412 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
413 return 42;
417 return L;
420 X x;
421 auto L = x.test();
422 L('4');
423 auto M = L('3');
424 M('a');
425 auto N = M('x');
426 N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
427 char (*np)(const char*, int, const char*, double, const char*, int) = N;
428 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
432 struct X {
433 static void foo(double d) { }
434 auto test() {
435 auto L = [](auto a) {
436 print("a = ", a, "\n");
437 foo(a);
438 return [](decltype(a) b) {
439 foo(b);
440 foo(sizeof(a) + sizeof(b));
441 return [](auto ... c) {
442 print("c = ", c ..., "\n");
443 foo(decltype(b){});
444 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
445 return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
446 print("d = ", d ..., "\n");
447 foo(decltype(b){});
448 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
449 return decltype(a){};
454 return L;
457 X x;
458 auto L = x.test();
459 L('4');
460 auto M = L('3');
461 M('a');
462 auto N = M('x');
463 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
464 char (*np)(const char*, int, const char*, double, const char*, int) = O;
465 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
466 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
469 } // end test()
471 namespace wrapped_within_templates {
473 namespace explicit_return {
474 template<class T> int fooT(T t) {
475 auto L = [](auto a) -> void {
476 auto M = [](char b) -> void {
477 auto N = [](auto c) -> void {
478 int x = 0;
479 x = sizeof(a);
480 x = sizeof(b);
481 x = sizeof(c);
483 N('a');
484 N(decltype(a){});
487 L(t);
488 L(3.14);
489 return 0;
492 int run = fooT('a') + fooT(3.14);
494 } // end explicit_return
496 namespace implicit_return_deduction {
497 template<class T> auto fooT(T t) {
498 auto L = [](auto a) {
499 auto M = [](char b) {
500 auto N = [](auto c) {
501 int x = 0;
502 x = sizeof(a);
503 x = sizeof(b);
504 x = sizeof(c);
506 N('a');
507 N(decltype(a){});
510 L(t);
511 L(3.14);
512 return 0;
515 int run = fooT('a') + fooT(3.14);
517 template<class ... Ts> void print(Ts ... ts) { }
519 template<class ... Ts> auto fooV(Ts ... ts) {
520 auto L = [](auto ... a) {
521 auto M = [](decltype(a) ... b) {
522 auto N = [](auto c) {
523 int x = 0;
524 x = sizeof...(a);
525 x = sizeof...(b);
526 x = sizeof(c);
528 N('a');
529 N(N);
530 N(first<Ts...>{});
532 M(a...);
533 print("a = ", a..., "\n");
535 L(L, ts...);
536 print("ts = ", ts..., "\n");
537 return 0;
540 int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
542 } //implicit_return_deduction
545 } //wrapped_within_templates
547 namespace at_ns_scope {
548 void foo(double d) { }
549 auto test() {
550 auto L = [](auto a) {
551 print("a = ", a, "\n");
552 foo(a);
553 return [](decltype(a) b) {
554 foo(b);
555 foo(sizeof(a) + sizeof(b));
556 return [](auto ... c) {
557 print("c = ", c ..., "\n");
558 foo(decltype(b){});
559 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
560 return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
561 print("d = ", d ..., "\n");
562 foo(decltype(b){});
563 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
564 return decltype(a){};
569 return L;
571 auto L = test();
572 auto L_test = L('4');
573 auto M = L('3');
574 auto M_test = M('a');
575 auto N = M('x');
576 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
577 char (*np)(const char*, int, const char*, double, const char*, int) = O;
578 auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
579 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
585 namespace variadic_tests_1 {
586 template<class ... Ts> void print(Ts ... ts) { }
588 template<class F, class ... Rest> F& FirstArg(F& f, Rest...) { return f; }
590 template<class ... Ts> int fooV(Ts ... ts) {
591 auto L = [](auto ... a) -> void {
592 auto M = [](decltype(a) ... b) -> void {
593 auto N = [](auto c) -> void {
594 int x = 0;
595 x = sizeof...(a);
596 x = sizeof...(b);
597 x = sizeof(c);
599 N('a');
600 N(N);
601 N(first<Ts...>{});
603 M(a...);
604 print("a = ", a..., "\n");
606 L(L, ts...);
607 print("ts = ", ts..., "\n");
608 return 0;
611 int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
613 namespace more_variadic_1 {
615 template<class ... Ts> int fooV(Ts ... ts) {
616 auto L = [](auto ... a) {
617 auto M = [](decltype(a) ... b) -> void {
618 auto N = [](auto c) -> void {
619 int x = 0;
620 x = sizeof...(a);
621 x = sizeof...(b);
622 x = sizeof(c);
624 N('a');
625 N(N);
626 N(first<Ts...>{});
628 M(a...);
629 return M;
631 auto M = L(L, ts...);
632 decltype(L(L, ts...)) (*fp)(decltype(L), decltype(ts) ...) = L;
633 void (*fp2)(decltype(L), decltype(ts) ...) = L(L, ts...);
636 auto L = [](auto ... a) {
637 auto M = [](decltype(a) ... b) {
638 auto N = [](auto c) -> void {
639 int x = 0;
640 x = sizeof...(a);
641 x = sizeof...(b);
642 x = sizeof(c);
644 N('a');
645 N(N);
646 N(first<Ts...>{});
647 return N;
649 M(a...);
650 return M;
652 auto M = L(L, ts...);
653 decltype(L(L, ts...)) (*fp)(decltype(L), decltype(ts) ...) = L;
654 fp(L, ts...);
655 decltype(L(L, ts...)(L, ts...)) (*fp2)(decltype(L), decltype(ts) ...) = L(L, ts...);
656 fp2 = fp(L, ts...);
657 void (*fp3)(char) = fp2(L, ts...);
658 fp3('a');
660 return 0;
663 int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
666 } //end ns more_variadic_1
668 } // end ns variadic_tests_1
670 namespace at_ns_scope_within_class_member {
671 struct X {
672 static void foo(double d) { }
673 auto test() {
674 auto L = [](auto a) {
675 print("a = ", a, "\n");
676 foo(a);
677 return [](decltype(a) b) {
678 foo(b);
679 foo(sizeof(a) + sizeof(b));
680 return [](auto ... c) {
681 print("c = ", c ..., "\n");
682 foo(decltype(b){});
683 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
684 return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
685 print("d = ", d ..., "\n");
686 foo(decltype(b){});
687 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
688 return decltype(a){};
693 return L;
696 X x;
697 auto L = x.test();
698 auto L_test = L('4');
699 auto M = L('3');
700 auto M_test = M('a');
701 auto N = M('x');
702 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
703 char (*np)(const char*, int, const char*, double, const char*, int) = O;
704 auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
705 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
707 } //end at_ns_scope_within_class_member
710 namespace at_ns_scope_within_class_template_member {
711 struct X {
712 static void foo(double d) { }
713 template<class T = int>
714 auto test(T = T{}) {
715 auto L = [](auto a) {
716 print("a = ", a, "\n");
717 foo(a);
718 return [](decltype(a) b) {
719 foo(b);
720 foo(sizeof(a) + sizeof(b));
721 return [](auto ... c) {
722 print("c = ", c ..., "\n");
723 foo(decltype(b){});
724 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
725 return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
726 print("d = ", d ..., "\n");
727 foo(decltype(b){});
728 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
729 return decltype(a){};
734 return L;
738 X x;
739 auto L = x.test();
740 auto L_test = L('4');
741 auto M = L('3');
742 auto M_test = M('a');
743 auto N = M('x');
744 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
745 char (*np)(const char*, int, const char*, double, const char*, int) = O;
746 auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
747 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
749 } //end at_ns_scope_within_class_member
752 namespace nested_generic_lambdas_123 {
753 void test() {
754 auto L = [](auto a) -> int {
755 auto M = [](auto b, decltype(a) b2) -> int {
756 return 1;
758 M(a, a);
760 L(3);
762 template<class T> void foo(T) {
763 auto L = [](auto a) { return a; };
765 template void foo(int);
766 } // end ns nested_generic_lambdas_123
768 namespace nested_fptr_235 {
769 int test()
771 auto L = [](auto b) {
772 return [](auto a) ->decltype(a) { return a; };
774 int (*fp)(int) = L(8);
775 fp(5);
776 L(3);
777 char (*fc)(char) = L('a');
778 fc('b');
779 L('c');
780 double (*fd)(double) = L(3.14);
781 fd(3.14);
782 fd(6.26);
783 return 0;
785 int run = test();
789 namespace fptr_with_decltype_return_type {
790 template<class F, class ... Rest> F& FirstArg(F& f, Rest& ... r) { return f; };
791 template<class ... Ts> auto vfun(Ts&& ... ts) {
792 print(ts...);
793 return FirstArg(ts...);
795 int test()
798 auto L = [](auto ... As) {
799 return [](auto b) ->decltype(b) {
800 vfun([](decltype(As) a) -> decltype(a) { return a; } ...)(first<decltype(As)...>{});
801 return decltype(b){};
804 auto LL = L(1, 'a', 3.14, "abc");
805 LL("dim");
807 return 0;
809 int run = test();
812 } // end ns nested_non_capturing_lambda_tests
814 namespace PR17476 {
815 struct string {
816 string(const char *__s) { }
817 string &operator+=(const string &__str) { return *this; }
820 template <class T>
821 void finalizeDefaultAtomValues() {
822 auto startEnd = [](const char * sym) -> void {
823 string start("__");
824 start += sym;
826 startEnd("preinit_array");
829 void f() { finalizeDefaultAtomValues<char>(); }
833 namespace PR17476_variant {
834 struct string {
835 string(const char *__s) { }
836 string &operator+=(const string &__str) { return *this; }
839 template <class T>
840 void finalizeDefaultAtomValues() {
841 auto startEnd = [](const T *sym) -> void {
842 string start("__");
843 start += sym;
845 startEnd("preinit_array");
848 void f() { finalizeDefaultAtomValues<char>(); }
852 namespace PR17877_lambda_declcontext_and_get_cur_lambda_disconnect {
855 template<class T> struct U {
856 int t = 0;
859 template<class T>
860 struct V {
861 U<T> size() const { return U<T>{}; }
864 template<typename T>
865 void Do() {
866 V<int> v{};
867 [=] { v.size(); };
872 namespace inclass_lambdas_within_nested_classes {
873 namespace ns1 {
875 struct X1 {
876 struct X2 {
877 enum { E = [](auto i) { return i; }(3) }; //expected-error{{inside of a constant expression}}\
878 //expected-error{{constant}}\
879 //expected-note{{non-literal type}}
880 int L = ([] (int i) { return i; })(2);
881 void foo(int i = ([] (int i) { return i; })(2)) { }
882 int B : ([](int i) { return i; })(3); //expected-error{{inside of a constant expression}}\
883 //expected-error{{not an integral constant}}\
884 //expected-note{{non-literal type}}
885 int arr[([](int i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
886 //expected-error{{must have a constant size}}
887 int (*fp)(int) = [](int i) { return i; };
888 void fooptr(int (*fp)(char) = [](char c) { return 0; }) { }
889 int L2 = ([](auto i) { return i; })(2);
890 void fooG(int i = ([] (auto i) { return i; })(2)) { }
891 int BG : ([](auto i) { return i; })(3); //expected-error{{inside of a constant expression}} \
892 //expected-error{{not an integral constant}}\
893 //expected-note{{non-literal type}}
894 int arrG[([](auto i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
895 //expected-error{{must have a constant size}}
896 int (*fpG)(int) = [](auto i) { return i; };
897 void fooptrG(int (*fp)(char) = [](auto c) { return 0; }) { }
900 } //end ns
902 namespace ns2 {
903 struct X1 {
904 template<class T>
905 struct X2 {
906 int L = ([] (T i) { return i; })(2);
907 void foo(int i = ([] (int i) { return i; })(2)) { }
908 int B : ([](T i) { return i; })(3); //expected-error{{inside of a constant expression}}\
909 //expected-error{{not an integral constant}}\
910 //expected-note{{non-literal type}}
911 int arr[([](T i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
912 //expected-error{{must have a constant size}}
913 int (*fp)(T) = [](T i) { return i; };
914 void fooptr(T (*fp)(char) = [](char c) { return 0; }) { }
915 int L2 = ([](auto i) { return i; })(2);
916 void fooG(T i = ([] (auto i) { return i; })(2)) { }
917 int BG : ([](auto i) { return i; })(3); //expected-error{{not an integral constant}}\
918 //expected-note{{non-literal type}}\
919 //expected-error{{inside of a constant expression}}
920 int arrG[([](auto i) { return i; })(3)]; //expected-error{{must have a constant size}} \
921 //expected-error{{inside of a constant expression}}
922 int (*fpG)(T) = [](auto i) { return i; };
923 void fooptrG(T (*fp)(char) = [](auto c) { return 0; }) { }
924 template<class U = char> int fooG2(T (*fp)(U) = [](auto a) { return 0; }) { return 0; }
925 template<class U = char> int fooG3(T (*fp)(U) = [](auto a) { return 0; });
928 template<class T>
929 template<class U>
930 int X1::X2<T>::fooG3(T (*fp)(U)) { return 0; }
931 X1::X2<int> x2; //expected-note {{in instantiation of}}
932 int run1 = x2.fooG2();
933 int run2 = x2.fooG3();
934 } // end ns
938 } //end ns inclass_lambdas_within_nested_classes
940 namespace pr21684_disambiguate_auto_followed_by_ellipsis_no_id {
941 int a = [](auto ...) { return 0; }();
944 namespace PR22117 {
945 int x = [](auto) {
946 return [](auto... run_args) {
947 using T = int(decltype(run_args)...);
948 return 0;
950 }(0)(0);
953 namespace PR41139 {
954 int y = [](auto outer) {
955 return [](auto inner) {
956 using T = int(decltype(outer), decltype(inner));
957 return 0;
959 }(0)(0);
962 namespace PR23716 {
963 template<typename T>
964 auto f(T x) {
965 auto g = [](auto&&... args) {
966 auto h = [args...]() -> int {
967 return 0;
969 return h;
971 return g;
974 auto x = f(0)();
977 namespace PR13987 {
978 class Enclosing {
979 void Method(char c = []()->char {
980 int d = [](auto x)->int {
981 struct LocalClass {
982 int Method() { return 0; }
984 return 0;
985 }(0);
986 return d; }()
990 class Enclosing2 {
991 void Method(char c = [](auto x)->char {
992 int d = []()->int {
993 struct LocalClass {
994 int Method() { return 0; }
996 return 0;
997 }();
998 return d; }(0)
1002 class Enclosing3 {
1003 void Method(char c = [](auto x)->char {
1004 int d = [](auto y)->int {
1005 struct LocalClass {
1006 int Method() { return 0; }
1008 return 0;
1009 }(0);
1010 return d; }(0)
1015 namespace PR32638 {
1016 //https://bugs.llvm.org/show_bug.cgi?id=32638
1017 void test() {
1018 [](auto x) noexcept(noexcept(x)) { } (0);
1022 namespace PR46637 {
1023 auto x = [](auto (*p)()) { return p(); };
1024 auto y = [](auto (*p)() -> auto) { return p(); };
1025 int f();
1026 void *v = x(f); // expected-error {{cannot initialize a variable of type 'void *' with an rvalue of type 'int'}}
1027 void *w = y(f); // expected-error {{cannot initialize a variable of type 'void *' with an rvalue of type 'int'}}