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