My linter claims that the lambda in the following code implicitly captures this
(and wants to have that capture specified explicitly). Is that correct?
template <class X>
struct OtherTemplate
{
static X bar()
{
return 1;
}
};
template <class Y>
struct Test
{
void foo()
{
auto lambda = [&]() {
// return (Y)1;
// return std::min<Y>(1, 2);
return OtherTemplate<Y>::bar();
};
(void)lambda;
}
};
// Explicit instantiation
template struct Test<int>;
There is no complaint if the lambda has no default capture, or if any of the commented lines are used instead. Removing the "templatedness" from OtherTemplate
also produces no diagnostic.
It seems obvious to me that this
is not used in the lambda. However, the following two questions seem to have observed similar behavior:
The C++11 standard (to pick one) indicates to me that capturing this
would require ODR-using this
([expr.prim.lambda]/11
). I don't see how that happens here, but I've been surprised before by the technicalities of template name lookups.
Edit: Due to discussions in the comments, I would like to emphasize the following note from the standard, in particular the can:
Implicit odr-uses of
this
can result in implicit capture.
That should make clear that a capture default does not unilaterally capture this
.
So before I file a bug with the linter, I wanted to make sure:
Does the above lambda implicitly capture this
?
I am most interested in C++17, but I would appreciate answers also covering the other standard versions.