0

I'm new to C++ and while learning and documenting about the subject, I stumbled accross something that intrigued me :

[this](type argument)
{
    // code
}

What does [this] stand for in this function ?

Here's the whole function in which I found this :

  void do_accept()
  {
    acceptor_.async_accept(socket_,
        [this](boost::system::error_code ec)
        {
          if (!ec)
          {
            std::make_shared<chat_session>(std::move(socket_), room_)->start();
          }

          do_accept();
        });
  }

(It's one of the boost.asio's asynchronous server example)

il0venoobs
  • 147
  • 1
  • 3
  • 8
  • It's a lambda capture specifier. It allows methods from the enclosing object to be used in the lambda. See [here](http://en.cppreference.com/w/cpp/language/lambda) – Yuushi Aug 06 '14 at 06:31
  • Thank you, I'll document on that ! – il0venoobs Aug 06 '14 at 06:32
  • Interestingly, despite me marking this as a duplicate, the linked answer does not go over the `this` capture. However, I figure that the answers there 1) could be updated, 2) are much more valuable. – Rapptz Aug 06 '14 at 06:32

0 Answers0