You are on page 1of 1

Capture *this in lambda expression: Timeline of change

Following is a list of captures relevant to capturing *this since C++11. Note that the below captures
can include other variables also from the lambda's context (e.g., [this] could be [this, i, j]), as
permitted. We are keeping it simple here for the sake of brevity.

Ÿ [this] — Captures *this by reference or &(*this).


Ÿ [*this]— Captures *this by value. By-value capture of *this is introduced in C++17.
Ÿ [this, *this] — Invalid. Cannot have this more than once.
Ÿ [&] — The reference-capture-default can implicitly capture this or &(*this).
Ÿ [&, this] — Same as [&], therefore redundant.
Ÿ [&, *this] — Valid since C++17, but unlikely to be used.
Ÿ [=] — The value-capture-default can implicitly capture this or &(*this). However, the implicit capture
of this through [=] is deprecated in C++20.
Ÿ [=, this] — Valid only since C++20. C++20 deprecates the implicit capture of this via [=] and allows
the explicit capture of this in combination with [=].
Ÿ [=, *this] — Valid since C++17.

C++11/C++14 C++17 C++20

[this] &(*this) &(*this) &(*this)

[*this] *this *this


C++17 change

[this,*this]

[&] Implicit &(*this) Implicit &(*this) Implicit &(*this)

[&, this] &(*this) &(*this) &(*this)

[&, *this] *this *this


C++17 change

Deprecated
[=] Implicit &(*this) Implicit &(*this) Implicit &(*this)
C++20 change

[=, this] &(*this)


C++20 change

[=, *this] *this *this


C++17 change

https://www.nextptr.com/tutorial/ta1430524603/capture-this-in-lambda-expression-timeline-of-change

You might also like