0

I was reading through the source code from Facebook's Proxygen, and I noticed some syntax I'd never seen before:

std::thread t([&] () {
    server.start();
});

Source: https://github.com/facebook/proxygen/blob/master/proxygen/httpserver/samples/push/PushServer.cpp

Is it an anonymous method? And what is the value of &, is it the thread?

It looks confusing because if it is an anonymous method how is server accessible, isn't it out of scope?

BugHunterUK
  • 7,707
  • 12
  • 55
  • 103

1 Answers1

1

You are right, this code uses anonymous function. The basic syntax is : [capture-list] (list-params) { function-body}. So what [&] does is

captures all automatic variables odr-used in the body of the lambda by reference

You can read more here

ig-melnyk
  • 2,679
  • 2
  • 20
  • 33