Alexandru Irimiea
Alexandru Irimiea

Reputation: 2634

Calling function in capture list of a lambda function

In this boost asio example I see:

auto self(shared_from_this()); //boost::shared_ptr<connection>

boost::asio::async_write(socket_, reply_.to_buffers(),
    [this, self](boost::system::error_code ec, std::size_t)
    {
        //...
    }
);

In Visual Studio 2015, if I write

[this, shared_from_this()](boost::system::error_code ec, std::size_t)

I receive the following error:

Error C2059 syntax error: ')'

Why can't the lambda function capture the boost::shared_ptr variable directly from the call to the shared_from_this()? Isn't it the same thing? I can't find anywhere an explanation. I've read other examples (e.g. this or this, but they don't ask this question).

Upvotes: 1

Views: 365

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118340

The correct syntax for named captures, in your case, would be:

[this, self=shared_from_this()]( ... ) 

Upvotes: 1

Related Questions