January 2018
Intermediate to advanced
374 pages
9h 53m
English
To understand what's going in the above example, take a look at how the compiler sees the previous lambda objects:
|
Capture by value case |
Capture by reference case |
class MutatingLambda {public: MutatingLambda(int m) : v{m} {} auto operator()() { std::cout<< v <<" "; ++v; }private: int v{};}; |
class MutatingLambda {public: MutatingLambda(int& m) : v{m} {} auto operator()()const{ std::cout<< v <<" "; ++v; }private: int& v;}; |
As you can see, the first case corresponds to a class with a regular member, whereas the capture by reference case simply corresponds to a class where the member variable is a reference.