C++11 lambda capture by value captures at declaration point

Viewed 15505

The code below prints 0, but I expect to see a 1. My conclusion is that lambda functions are not invoked by actually passing captured parameters to the functions, which is more intuitive. Am I right or am I missing something?

#include <iostream>
int main(int argc, char **argv){
  int value = 0;
  auto incr_value  = [&value]() { value++; };
  auto print_value = [ value]() { std::cout << value << std::endl; };
  incr_value();
  print_value();
  return 0;
}
3 Answers
Related