Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit this to lambda #7492

Closed
wants to merge 1 commit into from
Closed

Add explicit this to lambda #7492

wants to merge 1 commit into from

Conversation

Chocobo1
Copy link
Contributor

Implicit capture of this via [=] is deprecated in C++20 [-Wdeprecated]

@arvidn
Copy link
Owner

arvidn commented Aug 23, 2023

clang-tidy flags this at: https://github.com/arvidn/libtorrent/actions/runs/5951657979/job/16141913252?pr=7492#step:7:100
as

/home/runner/work/libtorrent/libtorrent/src/session_impl.cpp:5895:10: error: explicit capture of 'this' with a capture default of '=' is a C++20 extension [clang-diagnostic-c++20-extensions,-warnings-as-errors]
                        , [=, this](aux::listen_socket_handle const& sock
                              ^

Is explicit capture of this not supported before C++20? If so, it seems a bit drastic to require C++20 support, rather than suppressing the deprecation warning

Implicit capture of `this` via `[=]` is deprecated in C++20 [-Wdeprecated]
@Chocobo1
Copy link
Contributor Author

Is explicit capture of this not supported before C++20?

Sadly this seems to be the case. From https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture:

struct S2 { void f(int i); };
void S2::f(int i)
{
   [=, this] {};  // until C++20: Error: this when = is the default
                  // since C++20: OK, same as [=]
}

Although clang doesn't produce a hard error, there is still a warning: https://godbolt.org/z/o8Eh91Wx4

Now I've updated the PR to include a conditional to ensure compatibility with all cpp versions.

@arvidn
Copy link
Owner

arvidn commented Aug 24, 2023

I think it might be a mistake that this is captured in some of those cases

@arvidn
Copy link
Owner

arvidn commented Aug 24, 2023

@Chocobo1 does this work for you? #7502

@Chocobo1
Copy link
Contributor Author

Superseded by #7502.

@Chocobo1 Chocobo1 closed this Aug 24, 2023
@Chocobo1 Chocobo1 deleted the this branch August 24, 2023 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants