Clang warning: implicit capture of 'this' with a capture default of '=' is deprecated [-Wdeprecated-this-capture] (warn_deprecated_this_capture)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: implicit capture of 'this' with a capture default of '=' is deprecated (since 7.0)
Type Warning
Category Deprecations (since 7.0)
Internal Id warn_deprecated_this_capture (since 7.0)
Active by Default Yes
Flags -Wno-deprecated (43 elements)
-Wno-deprecated-this-capture (1 element)
Internal Message implicit capture of 'this' with a capture default of '=' is deprecated (since 7.0)
Regular Expression (?:warning|error|fatal error)\: implicit capture of 'this' with a capture default of '\=' is deprecated \[(?:\-Werror,)?\-Wdeprecated\-this\-capture[^\]]*\]
First Commit 2018-07-07 d82201e7c66b P0806R2 Implicit capture of this with a capture-default of [=] is

Description

The warning is issued by the Clang compiler when an implicit capture of this pointer occurs in a lambda with a capture default of =. This behavior is deprecated starting from the C++20 standard. In previous C++ standards, capturing this implicitly was not considered problematic. However, with C++20 and onwards, doing so without explicit mention is discouraged due to potential issues it might cause regarding the ownership and lifecycle of the captured this pointer. The deprecation serves as a caution to developers to ensure more deliberate handling of this in lambdas, especially to avoid unintentional copies or references that might lead to undefined behavior or dangling pointers. To resolve this warning, developers are advised to explicitly capture this by reference or by value, depending on the intended usage within the lambda body.  
AI Generated

Example

In the following example, a struct named C is defined with a member function f. Inside f, a lambda expression is declared with the capture default =, which means it implicitly captures local variables and the this pointer by value. The lambda's body contains a statement that creates an auto pointer p and assigns this to it. This illustrates the scenario where the this pointer is implicitly captured by the lambda, triggering the Clang warning about the deprecated implicit capture of this with a capture default of = in C++20 and onwards.  
AI Generated


Flags -std=c++2a -xc++ -Wdeprecated-this-capture

[Try out in Compiler Explorer]

Source
struct C {
  void f() {
    auto l = [=]() { auto p = this; }; // Implicit 'this' capture
  }
};
Compiler Output
<source>:3:31: warning: implicit capture of 'this' with a capture default of '=' is deprecated [-Wdeprecated-this-capture]
<source>:3:15: note: add an explicit capture of 'this' to capture '*this' by reference


Clang Internals (17.0.6)

Git Commit Message

P0806R2 Implicit capture of this with a capture-default of [=] is
deprecated.

Add a -Wdeprecated warning for this in C++2a onwards. (In C++17 and
before, there isn't a reasonable alternative because [=,this] is
ill-formed.)

llvm-svn: 336480

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaLambda.cpp (line 2036)

ExprResult Sema::BuildLambdaExpr(SourceLocation StartLoc, SourceLocation EndLoc, LambdaScopeInfo *LSI) {
  // ...
  {
    // ...
    for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) {
      // ...
      // Map the capture to our AST representation.
      LambdaCapture Capture = [&] {
        if (From.isThisCapture()) {
          // Capturing 'this' implicitly with a default of '[=]' is deprecated,
          // because it results in a reference capture. Don't warn prior to
          // C++2a; there's nothing that can be done about it before then.
          if (getLangOpts().CPlusPlus20 && IsImplicit && CaptureDefault == LCD_ByCopy) {
            Diag(From.getLocation(), diag::warn_deprecated_this_capture);

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/SemaCXX/lambda-implicit-this-capture.cpp

  • clang/test/SemaCXX/lambda-implicit-this-capture.cpp:11:14: warning: implicit capture of 'this' with a capture default of '=' is deprecated [-Wdeprecated-this-capture]