Add a utility to capture arguments by move even in C++11#26923
Add a utility to capture arguments by move even in C++11#26923ctiller merged 14 commits intogrpc:masterfrom
Conversation
Changes by create-pull-request action
Changes by create-pull-request action
Changes by create-pull-request action
|
|
||
| private: | ||
| GPR_NO_UNIQUE_ADDRESS F f_; | ||
| GPR_NO_UNIQUE_ADDRESS std::tuple<Captures...> captures_; |
There was a problem hiding this comment.
IWYU: add #include <tuple> for this line
| // | ||
| // BigThing big_thing; | ||
| // auto f = Capture( | ||
| // [](BigThing* c, int a, int b) { /*...*/ }, |
There was a problem hiding this comment.
This makes BigThing implicitly mutable within the capture itself. Either we want to disallow this (with forcing to use const BigThing *), or we probably need to explicitly document the behavior, which IMHO is a bit dangerous wrt multithreading, maybe?
BigThing big_thing;
auto f = capture(
[](BigThing* c, int a, int b) { c->MutateMe(); },
std::move(big_thing));| TEST(CaptureTest, WithArgsAndReturn) { | ||
| int captured = 1; | ||
| auto f = | ||
| Capture([captured](int* p, int arg) { return (captured + *p) * arg; }, 2); |
There was a problem hiding this comment.
This doesn't compile on Visual Studio with anything less than C++17... (I ran into this before myself)
(the tests we have aren't specifying this switch for msvc, which then defaults to "latest", and I think this is around C++20 for VS2019)
https://godbolt.org/z/GqEv446qz
error C2955: 'Capture': use of class template requires template argument list (x64 msvc v19.latest)
I'm not totally sure if we should care, but maybe that's problematic still.
(works with C++17 being selected: https://godbolt.org/z/KrEvjd574)
There was a problem hiding this comment.
https://godbolt.org/z/4x8cGrq45
wrapper function was missed on godbolt
* Add a utility to capture arguments by move even in C++11 * add test * Automated change: Fix sanity tests * missing file * Automated change: Fix sanity tests * better test * Automated change: Fix sanity tests * fmt Co-authored-by: ctiller <[email protected]>
* Add a utility to capture arguments by move even in C++11 * add test * Automated change: Fix sanity tests * missing file * Automated change: Fix sanity tests * better test * Automated change: Fix sanity tests * fmt Co-authored-by: ctiller <[email protected]>
@yashykt