Clang Project

clang_source_code/test/SemaCXX/cxx1y-generic-lambdas.cpp
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
6template<class F, class ...Rest> struct first_impl { typedef F type; };
7template<class ...Args> using first = typename first_impl<Args...>::type;
8
9namespace simple_explicit_capture {
10  void test() {
11    int i;
12    auto L = [i](auto a) { return i + a; };
13    L(3.14);
14  }
15}
16
17namespace explicit_call {
18int test() {
19  auto L = [](auto a) { return a; };
20  L.operator()(3);
21  L.operator()<char>(3.14); //expected-warning{{implicit conversion}}
22  return 0;
23}  
24} //end ns
25
26namespace test_conversion_to_fptr_2 {
27
28template<class T> struct X {
29
30  T (*fp)(T) = [](auto a) { return a; };
31  
32};
33
34X<int> xi;
35
36template<class T> 
37void fooT(T t, T (*fp)(T) = [](auto a) { return a; }) {
38  fp(t);
39}
40
41int test() {
42{
43  auto L = [](auto a) { return a; };
44  int (*fp)(int) = L;
45  fp(5);
46  L(3);
47  char (*fc)(char) = L;
48  fc('b');
49  L('c');
50  double (*fd)(double) = L;
51  fd(3.14);
52  fd(6.26);
53  L(4.25);
54}
55{
56  auto L = [](auto a) ->int { return a; }; //expected-note 2{{candidate template ignored}}
57  int (*fp)(int) = L;
58  char (*fc)(char) = L; //expected-error{{no viable conversion}}
59  double (*fd)(double) = L; //expected-error{{no viable conversion}}
60}
61{
62  int x = 5;
63  auto L = [=](auto b, char c = 'x') {
64    int i = x;
65    return [](auto a) ->decltype(a) { return a; };
66  };
67  int (*fp)(int) = L(8);
68  fp(5);
69  L(3);
70  char (*fc)(char) = L('a');
71  fc('b');
72  L('c');
73  double (*fd)(double) = L(3.14);
74  fd(3.14);
75  fd(6.26);
76
77}
78{
79 auto L = [=](auto b) {
80    return [](auto a) ->decltype(b)* { return (decltype(b)*)0; };
81  };
82  int* (*fp)(int) = L(8);
83  fp(5);
84  L(3);
85  char* (*fc)(char) = L('a');
86  fc('b');
87  L('c');
88  double* (*fd)(double) = L(3.14);
89  fd(3.14);
90  fd(6.26);
91}
92{
93 auto L = [=](auto b) {
94    return [](auto a) ->decltype(b)* { return (decltype(b)*)0; }; //expected-note{{candidate template ignored}}
95  };
96  char* (*fp)(int) = L('8');
97  fp(5);
98  char* (*fc)(char) = L('a');
99  fc('b');
100  double* (*fi)(int) = L(3.14);
101  fi(5);
102  int* (*fi2)(int) = L(3.14); //expected-error{{no viable conversion}}
103}
104
105{
106 auto L = [=](auto b) {
107    return [](auto a) { 
108      return [=](auto c) { 
109        return [](auto d) ->decltype(a + b + c + d) { return d; }; 
110      }; 
111    }; 
112  };
113  int (*fp)(int) = L('8')(3)(short{});
114  double (*fs)(char) = L(3.14)(short{})('4');
115}
116
117  fooT(3);
118  fooT('a');
119  fooT(3.14);
120  fooT("abcdefg");
121  return 0;
122}
123int run2 = test();
124
125}
126
127
128namespace test_conversion_to_fptr {
129
130void f1(int (*)(int)) { }
131void f2(char (*)(int)) { } // expected-note{{candidate}}
132void g(int (*)(int)) { } // #1 expected-note{{candidate}}
133void g(char (*)(char)) { } // #2 expected-note{{candidate}}
134void h(int (*)(int)) { } // #3
135void h(char (*)(int)) { } // #4
136
137int test() {
138{
139  auto glambda = [](auto a) { return a; };
140  glambda(1);
141  f1(glambda); // OK
142  f2(glambda); // expected-error{{no matching function}}
143  g(glambda); // expected-error{{call to 'g' is ambiguous}}
144  h(glambda); // OK: calls #3 since it is convertible from ID
145  
146  int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
147  
148}
149{
150  
151  auto L = [](auto a) { return a; };
152  int (*fp)(int) = L;
153  fp(5);
154  L(3);
155  char (*fc)(char) = L;
156  fc('b');
157  L('c');
158  double (*fd)(double) = L;
159  fd(3.14);
160  fd(6.26);
161  L(4.25);
162}
163{
164  auto L = [](auto a) ->int { return a; }; //expected-note 2{{candidate template ignored}}
165  int (*fp)(int) = L;
166  char (*fc)(char) = L; //expected-error{{no viable conversion}}
167  double (*fd)(double) = L; //expected-error{{no viable conversion}}
168}
169{
170  int* (*fp)(int*) = [](auto *a) -> auto* { return a; };
171  fp(0);
172}
173}
174
175namespace more_converion_to_ptr_to_function_tests {
176
177
178int test() {
179  {
180    int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
181    int (*fp2)(int) = [](auto b) -> int {  return b; };
182    int (*fp3)(char) = [](auto c) -> int { return c; };
183    char (*fp4)(int) = [](auto d) { return d; }; //expected-error{{no viable conversion}}\
184                                                 //expected-note{{candidate function [with $0 = int]}}
185    char (*fp5)(char) = [](auto e) -> int { return e; }; //expected-error{{no viable conversion}}\
186                                                 //expected-note{{candidate template ignored}}
187
188    fp2(3);
189    fp3('\n');
190    fp3('a');
191    return 0;
192  }
193} // end test()
194
195template<class ... Ts> void vfun(Ts ... ) { }
196
197int variadic_test() {
198
199 int (*fp)(int, char, double) = [](auto ... a) -> int { vfun(a...); return 4; };
200 fp(3, '4', 3.14);
201 
202 int (*fp2)(int, char, double) = [](auto ... a) { vfun(a...); return 4; };
203 fp(3, '4', 3.14);
204 return 2;
205}
206
207} // end ns
208
209namespace conversion_operator {
210  void test() {
211    auto L = [](auto a) -> int { return a; }; // expected-error {{cannot initialize}}
212    int (*fp)(int) = L; 
213    int (&fp2)(int) = [](auto a) { return a; };  // expected-error{{non-const lvalue}}
214    int (&&fp3)(int) = [](auto a) { return a; };  // expected-error{{no viable conversion}}\
215                                                  //expected-note{{candidate}}
216
217    using F = int(int);
218    using G = int(void*);
219    L.operator F*();
220    L.operator G*(); // expected-note-re {{instantiation of function template specialization '{{.*}}::operator()<void *>'}}
221
222    // Here, the conversion function is named 'operator auto (*)(int)', and
223    // there is no way to write that name in valid C++.
224    auto M = [](auto a) -> auto { return a; };
225    M.operator F*(); // expected-error {{no member named 'operator int (*)(int)'}}
226  }
227}
228}
229
230namespace return_type_deduction_ok {
231 auto l = [](auto a) ->auto { return a; }(2); 
232 auto l2 = [](auto a) ->decltype(auto) { return a; }(2);  
233 auto l3 = [](auto a) { return a; }(2); 
234
235}
236
237namespace generic_lambda_as_default_argument_ok {
238  void test(int i = [](auto a)->int { return a; }(3)) {
239  }
240}
241
242namespace nested_non_capturing_lambda_tests {
243template<class ... Ts> void print(Ts ...) { }
244int test() {
245{
246  auto L = [](auto a) {
247    return [](auto b) {
248      return b;
249    };
250  };
251  auto M = L(3);
252  M(4.15);
253 }
254{
255  int i = 10; //expected-note 3{{declared here}}
256  auto L = [](auto a) {
257    return [](auto b) { //expected-note 3{{begins here}}
258      i = b;  //expected-error 3{{cannot be implicitly captured}}
259      return b;
260    };
261  };
262  auto M = L(3); //expected-note{{instantiation}}
263  M(4.15); //expected-note{{instantiation}}
264 }
265 {
266  int i = 10; 
267  auto L = [](auto a) {
268    return [](auto b) { 
269      b = sizeof(i);  //ok 
270      return b;
271    };
272  };
273 }
274 {
275  auto L = [](auto a) {
276    print("a = ", a, "\n");
277    return [](auto b) ->decltype(a) {
278      print("b = ", b, "\n");
279      return b;
280    };
281  };
282  auto M = L(3);
283  M(4.15);
284 }
285 
286{
287  auto L = [](auto a) ->decltype(a) {
288    print("a = ", a, "\n");
289    return [](auto b) ->decltype(a) { //expected-error{{no viable conversion}}\
290                                      //expected-note{{candidate template ignored}}
291      print("b = ", b, "\n");
292      return b;
293    };
294  };
295  auto M = L(3); //expected-note{{in instantiation of}}
296 }
297{
298  auto L = [](auto a) {
299    print("a = ", a, "\n");
300    return [](auto ... b) ->decltype(a) {
301      print("b = ", b ..., "\n");
302      return 4;
303    };
304  };
305  auto M = L(3);
306  M(4.15, 3, "fv");
307}
308
309{
310  auto L = [](auto a) {
311    print("a = ", a, "\n");
312    return [](auto ... b) ->decltype(a) {
313      print("b = ", b ..., "\n");
314      return 4;
315    };
316  };
317  auto M = L(3);
318  int (*fp)(double, int, const char*) = M; 
319  fp(4.15, 3, "fv");
320}
321
322{
323  auto L = [](auto a) {
324    print("a = ", a, "\n");
325    return [](char b) {
326      return [](auto ... c) ->decltype(b) {
327        print("c = ", c ..., "\n");
328        return 42;
329      };
330    };
331  };
332  L(4);
333  auto M = L(3);
334  M('a');
335  auto N = M('x');
336  N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
337  char (*np)(const char*, int, const char*, double, const char*, int) = N;
338  np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
339}
340
341
342{
343  auto L = [](auto a) {
344    print("a = ", a, "\n");
345    return [](decltype(a) b) {
346      return [](auto ... c) ->decltype(b) {
347        print("c = ", c ..., "\n");
348        return 42;
349      };
350    };
351  };
352  L('4');
353  auto M = L('3');
354  M('a');
355  auto N = M('x');
356  N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
357  char (*np)(const char*, int, const char*, double, const char*, int) = N;
358  np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
359}
360
361
362{
363 struct X {
364  static void foo(double d) { } 
365  void test() {
366    auto L = [](auto a) {
367      print("a = ", a, "\n");
368      foo(a);
369      return [](decltype(a) b) {
370        foo(b);
371        foo(sizeof(a) + sizeof(b));
372        return [](auto ... c) ->decltype(b) {
373          print("c = ", c ..., "\n");
374          foo(decltype(b){});
375          foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
376          return 42;
377        };
378      };
379    };
380    L('4');
381    auto M = L('3');
382    M('a');
383    auto N = M('x');
384    N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
385    char (*np)(const char*, int, const char*, double, const char*, int) = N;
386    np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
387  }
388};
389X x;
390x.test();
391}
392// Make sure we can escape the function
393{
394 struct X {
395  static void foo(double d) { } 
396  auto test() {
397    auto L = [](auto a) {
398      print("a = ", a, "\n");
399      foo(a);
400      return [](decltype(a) b) {
401        foo(b);
402        foo(sizeof(a) + sizeof(b));
403        return [](auto ... c) ->decltype(b) {
404          print("c = ", c ..., "\n");
405          foo(decltype(b){});
406          foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
407          return 42;
408        };
409      };
410    };
411    return L;
412  }
413};
414  X x;
415  auto L = x.test();
416  L('4');
417  auto M = L('3');
418  M('a');
419  auto N = M('x');
420  N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
421  char (*np)(const char*, int, const char*, double, const char*, int) = N;
422  np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
423}
424
425{
426 struct X {
427  static void foo(double d) { } 
428  auto test() {
429    auto L = [](auto a) {
430      print("a = ", a, "\n");
431      foo(a);
432      return [](decltype(a) b) {
433        foo(b);
434        foo(sizeof(a) + sizeof(b));
435        return [](auto ... c) {
436          print("c = ", c ..., "\n");
437          foo(decltype(b){});
438          foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
439          return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
440            print("d = ", d ..., "\n");
441            foo(decltype(b){});
442            foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
443            return decltype(a){};
444          };
445        };
446      };
447    };
448    return L;
449  }
450};
451  X x;
452  auto L = x.test();
453  L('4');
454  auto M = L('3');
455  M('a');
456  auto N = M('x');
457  auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
458  char (*np)(const char*, int, const char*, double, const char*, int) = O;
459  np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
460  int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
461  
462}
463} // end test()
464
465namespace wrapped_within_templates {
466
467namespace explicit_return {
468template<class T> int fooT(T t) {
469  auto L = [](auto a) -> void { 
470    auto M = [](char b) -> void {
471      auto N = [](auto c) -> void {
472        int x = 0;
473        x = sizeof(a);        
474        x = sizeof(b);
475        x = sizeof(c);
476      };  
477      N('a');
478      N(decltype(a){});
479    };    
480  };
481  L(t);
482  L(3.14);
483  return 0;
484}
485
486int run = fooT('a') + fooT(3.14);
487
488} // end explicit_return
489
490namespace implicit_return_deduction {
491template<class T> auto fooT(T t) {
492  auto L = [](auto a)  { 
493    auto M = [](char b)  {
494      auto N = [](auto c)  {
495        int x = 0;
496        x = sizeof(a);        
497        x = sizeof(b);
498        x = sizeof(c);
499      };  
500      N('a');
501      N(decltype(a){});
502    };    
503  };
504  L(t);
505  L(3.14);
506  return 0;
507}
508
509int run = fooT('a') + fooT(3.14);
510
511template<class ... Ts> void print(Ts ... ts) { }
512
513template<class ... Ts> auto fooV(Ts ... ts) {
514  auto L = [](auto ... a) { 
515    auto M = [](decltype(a) ... b) {  
516      auto N = [](auto c) {
517        int x = 0;
518        x = sizeof...(a);        
519        x = sizeof...(b);
520        x = sizeof(c);
521      };  
522      N('a');
523      N(N);
524      N(first<Ts...>{});
525    };
526    M(a...);
527    print("a = ", a..., "\n");    
528  };
529  L(L, ts...);
530  print("ts = ", ts..., "\n");
531  return 0;
532}
533
534int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
535
536} //implicit_return_deduction
537
538
539} //wrapped_within_templates
540
541namespace at_ns_scope {
542  void foo(double d) { }
543  auto test() {
544    auto L = [](auto a) {
545      print("a = ", a, "\n");
546      foo(a);
547      return [](decltype(a) b) {
548        foo(b);
549        foo(sizeof(a) + sizeof(b));
550        return [](auto ... c) {
551          print("c = ", c ..., "\n");
552          foo(decltype(b){});
553          foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
554          return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
555            print("d = ", d ..., "\n");
556            foo(decltype(b){});
557            foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
558            return decltype(a){};
559          };
560        };
561      };
562    };
563    return L;
564  }
565auto L = test();
566auto L_test = L('4');
567auto M = L('3');
568auto M_test = M('a');
569auto N = M('x');
570auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
571char (*np)(const char*, int, const char*, double, const char*, int) = O;
572auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
573int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
574
575
576
577
578
579namespace variadic_tests_1 {
580template<class ... Ts> void print(Ts ... ts) { }
581
582template<class F, class ... Rest> F& FirstArg(F& f, Rest...) { return f; }
583 
584template<class ... Ts> int fooV(Ts ... ts) {
585  auto L = [](auto ... a) -> void { 
586    auto M = [](decltype(a) ... b) -> void {  
587      auto N = [](auto c) -> void {
588        int x = 0;
589        x = sizeof...(a);        
590        x = sizeof...(b);
591        x = sizeof(c);
592      };  
593      N('a');
594      N(N);
595      N(first<Ts...>{});
596    };
597    M(a...);
598    print("a = ", a..., "\n");    
599  };
600  L(L, ts...);
601  print("ts = ", ts..., "\n");
602  return 0;
603}
604
605int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
606
607namespace more_variadic_1 {
608
609template<class ... Ts> int fooV(Ts ... ts) {
610  auto L = [](auto ... a) { 
611    auto M = [](decltype(a) ... b) -> void {  
612      auto N = [](auto c) -> void {
613        int x = 0;
614        x = sizeof...(a);        
615        x = sizeof...(b);
616        x = sizeof(c);
617      };  
618      N('a');
619      N(N);
620      N(first<Ts...>{});
621    };
622    M(a...);
623    return M;
624  };
625  auto M = L(L, ts...);
626  decltype(L(L, ts...)) (*fp)(decltype(L), decltype(ts) ...) = L;
627  void (*fp2)(decltype(L), decltype(ts) ...) = L(L, ts...);
628  
629  {
630    auto L = [](auto ... a) { 
631      auto M = [](decltype(a) ... b) {  
632        auto N = [](auto c) -> void {
633          int x = 0;
634          x = sizeof...(a);        
635          x = sizeof...(b);
636          x = sizeof(c);
637        };  
638        N('a');
639        N(N);
640        N(first<Ts...>{});
641        return N;
642      };
643      M(a...);
644      return M;
645    };
646    auto M = L(L, ts...);
647    decltype(L(L, ts...)) (*fp)(decltype(L), decltype(ts) ...) = L;
648    fp(L, ts...);
649    decltype(L(L, ts...)(L, ts...)) (*fp2)(decltype(L), decltype(ts) ...) = L(L, ts...);
650    fp2 = fp(L, ts...);
651    void (*fp3)(char) = fp2(L, ts...);
652    fp3('a');
653  }
654  return 0;
655}
656
657int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
658
659
660} //end ns more_variadic_1
661
662} // end ns variadic_tests_1
663
664namespace at_ns_scope_within_class_member {
665 struct X {
666  static void foo(double d) { } 
667  auto test() {
668    auto L = [](auto a) {
669      print("a = ", a, "\n");
670      foo(a);
671      return [](decltype(a) b) {
672        foo(b);
673        foo(sizeof(a) + sizeof(b));
674        return [](auto ... c) {
675          print("c = ", c ..., "\n");
676          foo(decltype(b){});
677          foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
678          return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
679            print("d = ", d ..., "\n");
680            foo(decltype(b){});
681            foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
682            return decltype(a){};
683          };
684        };
685      };
686    };
687    return L;
688  }
689};
690X x;
691auto L = x.test();
692auto L_test = L('4');
693auto M = L('3');
694auto M_test = M('a');
695auto N = M('x');
696auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
697char (*np)(const char*, int, const char*, double, const char*, int) = O;
698auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
699int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
700  
701} //end at_ns_scope_within_class_member
702
703
704namespace at_ns_scope_within_class_template_member {
705 struct X {
706  static void foo(double d) { } 
707  template<class T = int>
708  auto test(T = T{}) {
709    auto L = [](auto a) {
710      print("a = ", a, "\n");
711      foo(a);
712      return [](decltype(a) b) {
713        foo(b);
714        foo(sizeof(a) + sizeof(b));
715        return [](auto ... c) {
716          print("c = ", c ..., "\n");
717          foo(decltype(b){});
718          foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
719          return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
720            print("d = ", d ..., "\n");
721            foo(decltype(b){});
722            foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
723            return decltype(a){};
724          };
725        };
726      };
727    };
728    return L;
729  }
730  
731};
732X x;
733auto L = x.test();
734auto L_test = L('4');
735auto M = L('3');
736auto M_test = M('a');
737auto N = M('x');
738auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
739char (*np)(const char*, int, const char*, double, const char*, int) = O;
740auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
741int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
742  
743} //end at_ns_scope_within_class_member
744
745
746namespace nested_generic_lambdas_123 {
747void test() {
748  auto L = [](auto a) -> int {
749    auto M = [](auto b, decltype(a) b2) -> int { 
750      return 1;
751    };
752    M(a, a);
753  };
754  L(3); 
755}
756template<class T> void foo(T) {
757 auto L = [](auto a) { return a; }; 
758}
759template void foo(int); 
760} // end ns nested_generic_lambdas_123
761
762namespace nested_fptr_235 {
763int test()
764{
765  auto L = [](auto b) {
766    return [](auto a) ->decltype(a) { return a; };
767  };
768  int (*fp)(int) = L(8);
769  fp(5);
770  L(3);
771  char (*fc)(char) = L('a');
772  fc('b');
773  L('c');
774  double (*fd)(double) = L(3.14);
775  fd(3.14);
776  fd(6.26);
777  return 0;
778}
779int run = test();
780}
781
782
783namespace fptr_with_decltype_return_type {
784template<class F, class ... Rest> F& FirstArg(F& f, Rest& ... r) { return f; };
785template<class ... Ts> auto vfun(Ts&& ... ts) {
786  print(ts...);
787  return FirstArg(ts...);
788}
789int test()
790{
791 {
792   auto L = [](auto ... As) {
793    return [](auto b) ->decltype(b) {   
794      vfun([](decltype(As) a) -> decltype(a) { return a; } ...)(first<decltype(As)...>{});
795      return decltype(b){};
796    };
797   };
798   auto LL = L(1, 'a', 3.14, "abc");
799   LL("dim");
800 }
801  return 0;
802}
803int run = test();
804}
805
806} // end ns nested_non_capturing_lambda_tests
807
808namespace PR17476 {
809struct string {
810  string(const char *__s) { }
811  string &operator+=(const string &__str) { return *this; }
812};
813
814template <class T> 
815void finalizeDefaultAtomValues() {
816  auto startEnd = [](const char * sym) -> void {
817    string start("__");
818    start += sym;
819  };
820  startEnd("preinit_array");
821}
822
823void f() { finalizeDefaultAtomValues<char>(); }
824
825
826
827namespace PR17476_variant {
828struct string {
829  string(const char *__s) { }
830  string &operator+=(const string &__str) { return *this; }
831};
832
833template <class T> 
834void finalizeDefaultAtomValues() {
835  auto startEnd = [](const T *sym) -> void {
836    string start("__");
837    start += sym;
838  };
839  startEnd("preinit_array");
840}
841
842void f() { finalizeDefaultAtomValues<char>(); }
843
844
845
846namespace PR17877_lambda_declcontext_and_get_cur_lambda_disconnect {
847
848
849template<class T> struct U {
850  int t = 0;
851};
852
853template<class T>
854struct V { 
855  U<T> size() const { return U<T>{}; }
856};
857
858template<typename T>
859void Do() {
860  V<int> v{};
861  [=] { v.size(); };
862}
863
864}
865
866namespace inclass_lambdas_within_nested_classes {
867namespace ns1 {
868
869struct X1 {  
870  struct X2 {
871    enum { E = [](auto i) { return i; }(3) }; //expected-error{{inside of a constant expression}}\
872                                          //expected-error{{constant}}\
873                                          //expected-note{{non-literal type}}
874    int L = ([] (int i) { return i; })(2);
875    void foo(int i = ([] (int i) { return i; })(2)) { }
876    int B : ([](int i) { return i; })(3); //expected-error{{inside of a constant expression}}\
877                                          //expected-error{{not an integral constant}}\
878                                          //expected-note{{non-literal type}}
879    int arr[([](int i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
880                                           //expected-error{{must have a constant size}}
881    int (*fp)(int) = [](int i) { return i; };
882    void fooptr(int (*fp)(char) = [](char c) { return 0; }) { }
883    int L2 = ([](auto i) { return i; })(2);
884    void fooG(int i = ([] (auto i) { return i; })(2)) { }
885    int BG : ([](auto i) { return i; })(3); //expected-error{{inside of a constant expression}}  \
886                                            //expected-error{{not an integral constant}}\
887                                            //expected-note{{non-literal type}}
888    int arrG[([](auto i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
889                                             //expected-error{{must have a constant size}}
890    int (*fpG)(int) = [](auto i) { return i; };
891    void fooptrG(int (*fp)(char) = [](auto c) { return 0; }) { }
892  };
893};
894} //end ns
895
896namespace ns2 {
897struct X1 {  
898  template<class T>
899  struct X2 {
900    int L = ([] (T i) { return i; })(2);
901    void foo(int i = ([] (int i) { return i; })(2)) { }
902    int B : ([](T i) { return i; })(3); //expected-error{{inside of a constant expression}}\
903                                        //expected-error{{not an integral constant}}\
904                                        //expected-note{{non-literal type}}
905    int arr[([](T i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
906                                         //expected-error{{must have a constant size}}
907    int (*fp)(T) = [](T i) { return i; };
908    void fooptr(T (*fp)(char) = [](char c) { return 0; }) { }
909    int L2 = ([](auto i) { return i; })(2);
910    void fooG(T i = ([] (auto i) { return i; })(2)) { }
911    int BG : ([](auto i) { return i; })(3); //expected-error{{not an integral constant}}\
912                                            //expected-note{{non-literal type}}\
913                                            //expected-error{{inside of a constant expression}}
914    int arrG[([](auto i) { return i; })(3)]; //expected-error{{must have a constant size}} \
915                                             //expected-error{{inside of a constant expression}}
916    int (*fpG)(T) = [](auto i) { return i; };
917    void fooptrG(T (*fp)(char) = [](auto c) { return 0; }) { }
918    template<class U = char> int fooG2(T (*fp)(U) = [](auto a) { return 0; }) { return 0; }
919    template<class U = char> int fooG3(T (*fp)(U) = [](auto a) { return 0; });
920  };
921};
922template<class T> 
923template<class U>
924int X1::X2<T>::fooG3(T (*fp)(U)) { return 0; } 
925X1::X2<int> x2; //expected-note {{in instantiation of}}
926int run1 = x2.fooG2();
927int run2 = x2.fooG3();
928} // end ns
929
930
931
932} //end ns inclass_lambdas_within_nested_classes
933
934namespace pr21684_disambiguate_auto_followed_by_ellipsis_no_id {
935int a = [](auto ...) { return 0; }();
936}
937
938namespace PR22117 {
939  int x = [](auto) {
940    return [](auto... run_args) {
941      using T = int(decltype(run_args)...);
942      return 0;
943    };
944  }(0)(0);
945}
946
947namespace PR23716 {
948template<typename T>
949auto f(T x) {
950  auto g = [](auto&&... args) {
951    auto h = [args...]() -> int {
952      return 0;
953    };
954    return h;
955  };
956  return g;
957}
958
959auto x = f(0)();
960}
961
962namespace PR13987 {
963class Enclosing {
964  void Method(char c = []()->char {
965    int d = [](auto x)->int {
966        struct LocalClass {
967          int Method() { return 0; }
968        };
969      return 0;
970    }(0);
971    return d; }()
972  );
973};
974
975class Enclosing2 {
976  void Method(char c = [](auto x)->char {
977    int d = []()->int {
978        struct LocalClass {
979          int Method() { return 0; }
980        };
981      return 0;
982    }();
983    return d; }(0)
984  );
985};
986
987class Enclosing3 {
988  void Method(char c = [](auto x)->char {
989    int d = [](auto y)->int {
990        struct LocalClass {
991          int Method() { return 0; }
992        };
993      return 0;
994    }(0);
995    return d; }(0)
996  );
997};
998}
999
1000namespace PR32638 {
1001 //https://bugs.llvm.org/show_bug.cgi?id=32638
1002 void test() {
1003    [](auto x) noexcept(noexcept(x)) { } (0);
1004 }
1005}
1006