Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Lambdas. They added it a while back.

  [things-to-capture](params) -> return-type { function-body; }
The return type is optional [0] since it can be detected automatically. The things-to-capture have a couple variants:

  [&foo]...
Captures variable foo by reference.

  [foo]...
Makes a copy of foo.

You can also capture "everything" using [&] or [=]. Handy if you don't want to make an explicit list of captured variables. You can also mix it:

  [&foo, bar]...
Captures foo by reference and a copy of bar.

https://en.cppreference.com/w/cpp/language/lambda

[0] Additionally, lambdas may not have a return value. Like this one:

  int n = 0;
  auto counter = [&n] () { n++; }
  counter();
  // now n == 1


Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: