Bug 1775161 Comment 0 Edit History

Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.

The implicit capture of `*this` when the capture default is `=` is deprecated in C++20:

https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture

See the attached log file for the 48 -Wdeprecated-this-capture warnings currently reported in mozilla-central.

However, due to inconsistencies in how clang and gcc handle explicit `this` captures in -std=c++17 mode, we can't fix these warnings (without ugly #ifdefs) to make this code compatible with both clang and gcc until after we build as -std=c++20 by default (bug 1768116).

```
class C {
  void memberFunction() {}

  void testCapture() {
    // Implicit capture of *this is OK in clang and gcc -std=c++17.
    // -Wdeprecated-this-capture warning in clang -std=c++20.
    // -Wdeprecated warning in gcc -std=c++20.
    [=]() { memberFunction(); }();

    // Explicit capture of *this is OK in clang and gcc -std=c++20.
    // -Wc++20-extensions warning in clang -std=c++17.
    // Error in gcc -std=c++17 with no way to suppress it.
    [=, this]() { memberFunction(); }();
  }
};
```
The implicit capture of `*this` when the capture default is `=` is deprecated in C++20:

https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture

See the attached log file for the 48 -Wdeprecated-this-capture warnings currently reported in mozilla-central.

However, due to inconsistencies in how clang and gcc handle explicit `this` captures in -std=c++17 mode, we can't fix these warnings (without ugly #ifdefs) to make this code compatible with both clang and gcc until after we build as -std=c++20 by default (bug 1768116).

```
class C {
  void memberFunction() {}

  void testCapture() {
    [=]() { memberFunction(); }();
    // Implicit capture of *this is OK in clang and gcc -std=c++17.
    // -Wdeprecated-this-capture warning in clang -std=c++20.
    // -Wdeprecated warning in gcc -std=c++20.

    [=, this]() { memberFunction(); }();
    // Explicit capture of *this is OK in clang and gcc -std=c++20.
    // -Wc++20-extensions warning in clang -std=c++17.
    // Error in gcc -std=c++17 with no way to suppress it.
  }
};
```

Back to Bug 1775161 Comment 0