Clang warning: implicit capture of 'this' with a capture default of '=' is deprecated [-Wdeprecated-this-capture] (warn_deprecated_this_capture)
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
Example
Flags | -std=c++2a -xc++ -Wdeprecated-this-capture
|
|
---|---|---|
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]