| Issue |
178723
|
| Summary |
[clang] Spurious `-Wunused-lambda-capture` when capturing `[this]` to call a member function
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
StephanTLavavej
|
Repros with Clang 20.1.8. Also repros with Clang 21.1.0 on Compiler Explorer: https://godbolt.org/z/zYbazqK6T
```
D:\GitHub\STL\out\x64>type meow.cpp
```
```cpp
#include <functional>
#include <utility>
template <class K, class V, class C>
struct flat_map_base {
flat_map_base() {
#ifdef SUPPRESS_WARNING_WITH_THIS_ARROW
auto pred = [this](const auto& l, const auto& r) { return !this->compare_keys(l.first, r.first); };
#elif defined(OMIT_THIS_CAPTURE_FOR_COMPILER_ERROR)
auto pred = [](const auto& l, const auto& r) { return !compare_keys(l.first, r.first); };
#else // repro the spurious warning
auto pred = [this](const auto& l, const auto& r) { return !compare_keys(l.first, r.first); };
#endif
std::pair<K, V> val;
pred(val, val);
}
template <class T, class U>
bool compare_keys(const T& left, const U& right) const {
return comp(left, right);
}
C comp;
};
int main() {
flat_map_base<int, int, std::less<int>> fm;
}
```
```
D:\GitHub\STL\out\x64>clang-cl -v
clang version 20.1.8
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\Microsoft Visual Studio\18\Insiders\VC\Tools\Llvm\x64\bin
```
```
D:\GitHub\STL\out\x64>clang-cl /EHsc /nologo /W4 /std:c++latest meow.cpp
meow.cpp(12,22): warning: lambda capture 'this' is not used [-Wunused-lambda-capture]
12 | auto pred = [this](const auto& l, const auto& r) { return !compare_keys(l.first, r.first); };
| ^
meow.cpp(28,45): note: in instantiation of member function 'flat_map_base<int, int, std::less<int>>::flat_map_base'
requested here
28 | flat_map_base<int, int, std::less<int>> fm;
| ^
1 warning generated.
```
Adding `this->` silences the warning as a workaround:
```
D:\GitHub\STL\out\x64>clang-cl /EHsc /nologo /W4 /std:c++latest /DSUPPRESS_WARNING_WITH_THIS_ARROW meow.cpp
```
Attempting to omit `[this]` emits a predictable (and correct) compiler error, showing that the capture is used and necessary:
```
D:\GitHub\STL\out\x64>clang-cl /EHsc /nologo /W4 /std:c++latest /DOMIT_THIS_CAPTURE_FOR_COMPILER_ERROR meow.cpp
meow.cpp(10,64): error: 'this' cannot be implicitly captured in this context
10 | auto pred = [](const auto& l, const auto& r) { return !compare_keys(l.first, r.first); };
| ^
meow.cpp(10,22): note: explicitly capture 'this'
10 | auto pred = [](const auto& l, const auto& r) { return !compare_keys(l.first, r.first); };
| ^
| this
1 error generated.
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs