p5-generic-lambda-1y.cpp revision ecb5819a9e64fb654d46a3b270a286cc570c58ff
1// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++1y -DCXX1Y -emit-llvm
2
3namespace test_factorial {
4
5auto Fact = [](auto Self, unsigned n) -> unsigned {
6    return !n ? 1 : Self(Self, n - 1) * n;
7};
8
9auto six = Fact(Fact, 3);
10
11}
12
13namespace overload_generic_lambda {
14  template <class F1, class F2> struct overload : F1, F2 {
15    using F1::operator();
16    using F2::operator();
17    overload(F1 f1, F2 f2) : F1(f1), F2(f2) { }
18  };
19
20  auto NumParams = [](auto Self, auto h, auto ... rest) -> unsigned {
21    return 1 + Self(Self, rest...);
22  };
23  auto Base = [](auto Self, auto h) -> unsigned {
24      return 1;
25  };
26  overload<decltype(Base), decltype(NumParams)> O(Base, NumParams);
27  int num_params =  O(O, 5, 3, "abc", 3.14, 'a');
28}
29
30
31namespace overload_generic_lambda_return_type_deduction {
32  template <class F1, class F2> struct overload : F1, F2 {
33    using F1::operator();
34    using F2::operator();
35    overload(F1 f1, F2 f2) : F1(f1), F2(f2) { }
36  };
37
38  auto NumParams = [](auto Self, auto h, auto ... rest) {
39    return 1 + Self(Self, rest...);
40  };
41  auto Base = [](auto Self, auto h) {
42      return 1;
43  };
44  overload<decltype(Base), decltype(NumParams)> O(Base, NumParams);
45  int num_params =  O(O, 5, 3, "abc", 3.14, 'a');
46}
47
48namespace test_standard_p5 {
49// FIXME: This test should eventually compile without an explicit trailing return type
50auto glambda = [](auto a, auto&& b) ->bool { return a < b; };
51bool b = glambda(3, 3.14); // OK
52
53}
54namespace test_deduction_failure {
55 int test() {
56   auto g = [](auto *a) { //expected-note{{candidate template ignored}}
57    return a;
58   };
59   struct X { };
60   X *x;
61   g(x);
62   g(3); //expected-error{{no matching function}}
63   return 0;
64 }
65
66}
67
68namespace test_instantiation_or_sfinae_failure {
69int test2() {
70  {
71    auto L = [](auto *a) {
72                return (*a)(a); }; //expected-error{{called object type 'double' is not a function}}
73    //l(&l);
74    double d;
75    L(&d); //expected-note{{in instantiation of}}
76    auto M = [](auto b) { return b; };
77    L(&M); // ok
78  }
79  {
80    auto L = [](auto *a) ->decltype (a->foo()) { //expected-note2{{candidate template ignored:}}
81                return (*a)(a); };
82    //l(&l);
83    double d;
84    L(&d); //expected-error{{no matching function for call}}
85    auto M = [](auto b) { return b; };
86    L(&M); //expected-error{{no matching function for call}}
87
88  }
89  return 0;
90}
91
92
93}
94
95namespace test_misc {
96auto GL = [](auto a, decltype(a) b) //expected-note{{candidate function}}
97                -> int { return a + b; };
98
99void test() {
100   struct X { };
101   GL(3, X{}); //expected-error{{no matching function}}
102}
103
104void test2() {
105  auto l = [](auto *a) -> int {
106              (*a)(a); return 0; }; //expected-error{{called object type 'double' is not a function}}
107  l(&l);
108  double d;
109  l(&d); //expected-note{{in instantiation of}}
110}
111
112}
113
114namespace nested_lambdas {
115  int test() {
116    auto L = [](auto a) {
117                 return [=](auto b) {  //expected-error{{unimplemented}}
118                           return a + b;
119                        };
120              };
121   // auto M = L(3.14);
122   // return M('4');
123  }
124  auto get_lambda() {
125    return [](auto a) {
126      return a;
127    };
128  };
129
130  int test2() {
131    auto L = get_lambda();
132    L(3);
133  }
134}
135
136