Skip to content

Add a utility to capture arguments by move even in C++11#26923

Merged
ctiller merged 14 commits intogrpc:masterfrom
ctiller:captcha
Aug 11, 2021
Merged

Add a utility to capture arguments by move even in C++11#26923
ctiller merged 14 commits intogrpc:masterfrom
ctiller:captcha

Conversation

@ctiller
Copy link
Copy Markdown
Member

@ctiller ctiller commented Aug 6, 2021

@ctiller ctiller added the release notes: no Indicates if PR should not be in release notes label Aug 6, 2021

private:
GPR_NO_UNIQUE_ADDRESS F f_;
GPR_NO_UNIQUE_ADDRESS std::tuple<Captures...> captures_;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IWYU: add #include <tuple> for this line

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

//
// BigThing big_thing;
// auto f = Capture(
// [](BigThing* c, int a, int b) { /*...*/ },
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a comment

TEST(CaptureTest, WithArgsAndReturn) {
int captured = 1;
auto f =
Capture([captured](int* p, int arg) { return (captured + *p) * arg; }, 2);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ctiller ctiller enabled auto-merge (squash) August 11, 2021 21:42
@ctiller ctiller merged commit bb4311b into grpc:master Aug 11, 2021
Vignesh2208 pushed a commit to Vignesh2208/grpc that referenced this pull request Aug 20, 2021
* 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]>
lidizheng pushed a commit to lidizheng/grpc that referenced this pull request Sep 23, 2021
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes: no Indicates if PR should not be in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants