[]的C++含义

标签 c++ boost boost-asio

<分区>

这是来自 boosts asio 的一个例子。 这是什么意思?为什么是 []?

acceptor_.async_accept(socket_,
    [this](boost::system::error_code ec)

最佳答案

这是一个lambda expression用于创建作为表达式的函数

[]是捕获列表

A list of symbols can be passed as follows:

  • [a,&b] where a is captured by value and b is captured by reference.
  • [this] captures the this pointer by value
  • [&] captures all automatic variables mentioned in the body of the lambda by reference
  • [=] captures all automatic variables mentioned in the body of the lambda by value
  • [] captures nothing

关于[]的C++含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22327248/

相关文章:

C++ lambda指针/引用内存占用之争

c++ - 关于alloca的使用和滥用

c++ - 使用 Boost::Asio 通过 TCP 读取二进制数据

c++ - ASIO 客户端服务器在同一台 PC 上连接正常,但在本地网络上失败

c++ - 无法使用 boost asio read() 获取所有数据

c++ - OpenGL - 不显示简单的 2D 纹理

c++ - 在 C++ 中定义运算符 **

c++ - boost asio 超时

c++ - Windows 控制台代码页 866

c++ - 安全的 enable_shared_from_this 用法