| 1 |
// P0018R3 - C++17 lambda capture of *this |
| 2 |
// { dg-do compile { target c++11 } } |
| 3 |
|
| 4 |
struct A { |
| 5 |
int a; |
| 6 |
void foo () { |
| 7 |
int v = 4; |
| 8 |
auto b = [*this, this] {}; // { dg-error "already captured 'this'" } |
| 9 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 10 |
auto c = [this, *this] {}; // { dg-error "already captured 'this'" } |
| 11 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 12 |
auto d = [this] { return a; }; |
| 13 |
auto e = [*this] { return a; }; // { dg-error "'*this' capture only available with" "" { target c++14_down } } |
| 14 |
auto f = [this] { a++; }; |
| 15 |
auto g = [*this] { a++; }; // { dg-error "in read-only object" } |
| 16 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 17 |
auto h = [*this] () mutable { a++; };// { dg-error "'*this' capture only available with" "" { target c++14_down } } |
| 18 |
auto i = [=] { return a; }; // { dg-warning "implicit capture" "" { target c++2a } } |
| 19 |
auto j = [&] { return a; }; |
| 20 |
// P0409R2 - C++2A lambda capture [=, this] |
| 21 |
auto k = [=, this] { return a; };// { dg-error "explicit by-copy capture of 'this' with by-copy capture default only available with" "" { target c++17_down } } |
| 22 |
auto l = [&, this] { return a; }; |
| 23 |
auto m = [=, *this] { return a; };// { dg-error "'*this' capture only available with" "" { target c++14_down } } |
| 24 |
auto n = [&, *this] { return a; };// { dg-error "'*this' capture only available with" "" { target c++14_down } } |
| 25 |
auto o = [*this, &v] { return a + v; };// { dg-error "'*this' capture only available with" "" { target c++14_down } } |
| 26 |
auto p = [*this] { this = 0; }; // { dg-error "lvalue required as left operand of assignment" } |
| 27 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 28 |
auto q = [=, this, *this] { return a; };// { dg-error "already captured 'this'" } |
| 29 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 30 |
// { dg-error "explicit by-copy capture of 'this' with by-copy capture default only available with" "" { target c++17_down } .-2 } |
| 31 |
auto r = [=, this, this] { return a; };// { dg-error "already captured 'this'" } |
| 32 |
// { dg-error "explicit by-copy capture of 'this' with by-copy capture default only available with" "" { target c++17_down } .-1 } |
| 33 |
auto s = [=, *this, this] { return a; };// { dg-error "already captured 'this'" } |
| 34 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 35 |
// { dg-error "explicit by-copy capture of 'this' with by-copy capture default only available with" "" { target c++17_down } .-2 } |
| 36 |
auto t = [=, *this, *this] { return a; };// { dg-error "already captured 'this'" } |
| 37 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 38 |
auto u = [&, this, *this] { return a; };// { dg-error "already captured 'this'" } |
| 39 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 40 |
auto w = [&, this, this] { return a; };// { dg-error "already captured 'this'" } |
| 41 |
auto x = [&, *this, this] { return a; };// { dg-error "already captured 'this'" } |
| 42 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 43 |
auto y = [&, *this, *this] { return a; };// { dg-error "already captured 'this'" } |
| 44 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 45 |
} |
| 46 |
}; |
| 47 |
struct B { |
| 48 |
double b; |
| 49 |
B () : b (.007) {} |
| 50 |
double foo () { |
| 51 |
return [this]{ return [*this] { return b; }; }()(); // { dg-error "'*this' capture only available with" "" { target c++14_down } } |
| 52 |
} |
| 53 |
void bar () { |
| 54 |
auto c = []{ return [*this] { return b; }; }; // { dg-error "'this' was not captured for this lambda function" } |
| 55 |
} // { dg-error "invalid use of non-static data member 'B::b'" "" { target *-*-* } .-1 } |
| 56 |
}; // { dg-error "'*this' capture only available with" "" { target c++14_down } .-2 } |
| 57 |
struct C { |
| 58 |
int c; |
| 59 |
C (const C &) = delete; |
| 60 |
void bar () const; |
| 61 |
void foo () { |
| 62 |
auto d = [this] { return c; }; |
| 63 |
auto e = [*this] { return c; }; // { dg-error "use of deleted function" } |
| 64 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 65 |
auto f = [=] { return c; }; // { dg-warning "implicit capture" "" { target c++2a } } |
| 66 |
auto g = [&] { return c; }; |
| 67 |
auto h = [this] { bar (); }; |
| 68 |
auto i = [*this] { bar (); }; // { dg-error "use of deleted function" } |
| 69 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 70 |
} |
| 71 |
}; |
| 72 |
struct D { |
| 73 |
int d; |
| 74 |
~D () = delete; |
| 75 |
void bar () const; |
| 76 |
void foo () { |
| 77 |
auto e = [this] { return d; }; |
| 78 |
auto f = [*this] { return d; }; // { dg-error "use of deleted function" } |
| 79 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 80 |
auto g = [=] { return d; }; // { dg-warning "implicit capture" "" { target c++2a } } |
| 81 |
auto h = [&] { return d; }; |
| 82 |
auto i = [this] { bar (); }; |
| 83 |
auto j = [*this] { bar (); }; // { dg-error "use of deleted function" } |
| 84 |
// { dg-error "'*this' capture only available with" "" { target c++14_down } .-1 } |
| 85 |
} |
| 86 |
}; |