Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

c++ - How do capture lists of lambdas actually work in C++11?

I know that capture lists make variables available inside a lambda function body like so:

int pos(0);
std::function<void()> incPos = [&pos](){ ++pos; };
incPos(); //pos is now 1

But how does that capturing actually work on compiler level? Where are the captured addresses or captured values stored?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Each lambda expression generates a unique function object (closure) that stores the captured variables as data members. For instance, the lambda expression in your code would be transformed into something like this by the compiler:

struct __uniquely_named_lambda
{
  __uniquely_named_lambda(int& pos)
  : pos(pos) {}
  int& pos;

  void operator()() const
  { ++pos; }
};

Invoking the lambda is simply a call to operator().

The data member is a reference since you captured by reference. If you captured by value it would be a plain int. Also note that generated operator() is const by default. This is why you cannot modify captured variables unless you use the mutable keyword.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...