When it comes to lambdas there is a lot more to what they can do than MS "explains." To make the situation worse lambdas have changed since they were first introduced in C++11.
https://en.cppreference.com/w/cpp/language/lambda
Scroll down to the "Lambda capture" section to get a better idea why there are errors and not-errors in your code snippet from MS.
To be honest even the explanation at cppreference is a bit convoluted IMO and needs some "talk to me as if I were a 3 year old"
Lucy 'splainin'.
The curse of self-teaching....
I would recommend an eBook that delves deep into the nitty-gritty on lambdas, from C++98 to C++20:
https://leanpub.com/cpplambda
Bundle it with the author's C++ Initialization eBook for a really good deal for quite a lot of C++ info.
https://leanpub.com/b/lambdacppmembers
See how things change with each C++ version is never a bad thing.
FYI, I own both eBooks and while I have not done a deep dive into either I have skimmed both and found the material to be more than worth the price.
If you've never run across C++ Stories you should take a peek:
https://www.cppstories.com/p/start-here/
One nice feature of leanpub is buying an eBook that is still being written is cheaper than buying a completed book, and you essentially get "free updates". Even books that are 100% complete can be updated by the author and you can get the newer version without paying more money.
Another author, Nicolai M. Josuttis, with books on leanpub I also own has announced a new book I can't wait for release: C++23 - The Complete Guide:
https://leanpub.com/cpp23
MS wrote: |
---|
The this pointer may be captured by value by specifying *this in the capture clause. |
That gets rearranged depending on which C++ standard your code is built against, something MS apparently doesn't address:
cppreference wrote: |
---|
If the capture-default is = , subsequent simple captures must begin with & or be *this (since C++17) or this (since C++20). |
1 2 3 4 5 6 7 8 9 10
|
struct S2 { void f(int i); };
void S2::f(int i)
{
[=] {}; // OK: by-copy capture default
[=, &i] {}; // OK: by-copy capture, except i is captured by reference
[=, *this] {}; // until C++17: Error: invalid syntax
// since C++17: OK: captures the enclosing S2 by copy
[=, this] {}; // until C++20: Error: this when = is the default
// since C++20: OK, same as [=]
}
| |
(From the "Lambda capture" section at
https://en.cppreference.com/w/cpp/language/lambda)