[英]What is [this, self] before handler assignment means in Boost library asio example?
I have never seen such syntax before "[this, self]", I used to program C and did a bit with C++, and now learning C++11 and Boost library, the compiler is happy with, but I can't figure out how it works and what it does. 我以前从未见过这样的语法“[this,self]”,我以前编程C并用C ++做了一点,现在学习C ++ 11和Boost库,编译器很满意,但我无法想象它是如何工作的以及它的作用。
void do_read()
{
auto self(shared_from_this());
socket_.async_read_some(boost::asio::buffer(data_, max_length),
[this, self](boost::system::error_code ec, std::size_t length)
---------^
{
if (!ec)
{
do_write(length);
}
});
}
It comes from Boost library Async Echo server example in this link 它来自此链接中的 Boost库Async Echo服务器示例
That is the capture-list of the lambda. 这是lambda的捕获列表 。 It is capturing the variable this
(pointer) and the variable self
declared one line above. 它捕获变量this
(指针)和变量self
声明上面的一行。
[...](...){...}
// A B C
The above is simplified syntax for the lambda in your code. 以上是代码中lambda的简化语法。 A
is the capture-list mentioned above (used to "capture" variables from the current scope). A
是上面提到的捕获列表(用于“捕获”当前范围内的变量)。 B
is the argument list (just like in a function) and C
is the body (again, like a function). B
是参数列表(就像在函数中一样), C
是正文(再次,就像一个函数)。
这个lambda捕获是为了确保会话对象超过异步操作:只要lambda处于活动状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.