C++ 语法,这是一个 lambda
C++ syntax, is this a lambda
我正在为测试过程构建测试自动化解决方案。我 运行 变成了 link,但我的 C++ 能力不是很好。有人可以解释下面的语法吗?
void read_loop(bp::async_pipe& p, mutable_buffer buf) {
p.async_read_some(buf, [&p,buf](std::error_code ec, size_t n) {
std::cout << "Received " << n << " bytes (" << ec.message() << "): '";
std::cout.write(boost::asio::buffer_cast<char const*>(buf), n) << "'" << std::endl;
if (!ec) read_loop(p, buf);
});
}
我的解决方案需要编排用于监控数据流量的二进制文件。有两个主要程序。一个管理其他程序,称之为B
。程序 A
只是存储所有数据。测试过程应该检查输出并匹配一些关键字。如果匹配,则测试通过。高级解决方案应该对所有程序执行 I/O——操纵状态,并匹配 'outputs'。
lambda 长什么样:
// This is the basics of a lambda expression.
[ /* Optional Capture Variables */ ]( /*Optional Parameters */ ) {
/* Optional Code */
}
所以最简单的 lambda 是:
[](){}
有哪些不同的部分:
Capture Variables: Variables from the current visible scope
that can be used by the lambda.
Parameters: Just like a normal function, these are parameters
that are passed to the parameter when it is invoked.
Code: This is the code that is executed and uses the
above mentioned variables when the lambda is invoked.
我认为考虑幕后可能(未指定)发生的事情是有用的。 Lamda 只是仿函数(可调用对象)的语法糖。
int main()
{
int data = 12;
// define the lambda.
auto lambda = [&data](int val){
std::cout << "Lambda: " << (val + data) << "\n";
};
lambda(2); // activate the lambda.
}
这在语义上等同于以下内容:
class CompilerGeneratedClassNameLambda
{
int& data;
public:
CompilerGeneratedClassNameLambda(int& data)
: data(data)
{}
void operator()(int val) const {
std::cout << "Lambda: " << (val + data) << "\n";
}
};
int main()
{
int data = 12;
CompilerGeneratedClassNameLambda lambda(data);
lambda(2); // activate the lambda.
}
在你的代码中。这部分是 lambda:
[&p,buf](std::error_code ec, size_t n) {
std::cout << "Received " << n << " bytes (" << ec.message() << "): '";
std::cout.write(boost::asio::buffer_cast<char const*>(buf), n) << "'" << std::endl;
if (!ec) read_loop(p, buf);
}
我正在为测试过程构建测试自动化解决方案。我 运行 变成了 link,但我的 C++ 能力不是很好。有人可以解释下面的语法吗?
void read_loop(bp::async_pipe& p, mutable_buffer buf) {
p.async_read_some(buf, [&p,buf](std::error_code ec, size_t n) {
std::cout << "Received " << n << " bytes (" << ec.message() << "): '";
std::cout.write(boost::asio::buffer_cast<char const*>(buf), n) << "'" << std::endl;
if (!ec) read_loop(p, buf);
});
}
我的解决方案需要编排用于监控数据流量的二进制文件。有两个主要程序。一个管理其他程序,称之为B
。程序 A
只是存储所有数据。测试过程应该检查输出并匹配一些关键字。如果匹配,则测试通过。高级解决方案应该对所有程序执行 I/O——操纵状态,并匹配 'outputs'。
lambda 长什么样:
// This is the basics of a lambda expression.
[ /* Optional Capture Variables */ ]( /*Optional Parameters */ ) {
/* Optional Code */
}
所以最简单的 lambda 是:
[](){}
有哪些不同的部分:
Capture Variables: Variables from the current visible scope
that can be used by the lambda.
Parameters: Just like a normal function, these are parameters
that are passed to the parameter when it is invoked.
Code: This is the code that is executed and uses the
above mentioned variables when the lambda is invoked.
我认为考虑幕后可能(未指定)发生的事情是有用的。 Lamda 只是仿函数(可调用对象)的语法糖。
int main()
{
int data = 12;
// define the lambda.
auto lambda = [&data](int val){
std::cout << "Lambda: " << (val + data) << "\n";
};
lambda(2); // activate the lambda.
}
这在语义上等同于以下内容:
class CompilerGeneratedClassNameLambda
{
int& data;
public:
CompilerGeneratedClassNameLambda(int& data)
: data(data)
{}
void operator()(int val) const {
std::cout << "Lambda: " << (val + data) << "\n";
}
};
int main()
{
int data = 12;
CompilerGeneratedClassNameLambda lambda(data);
lambda(2); // activate the lambda.
}
在你的代码中。这部分是 lambda:
[&p,buf](std::error_code ec, size_t n) {
std::cout << "Received " << n << " bytes (" << ec.message() << "): '";
std::cout.write(boost::asio::buffer_cast<char const*>(buf), n) << "'" << std::endl;
if (!ec) read_loop(p, buf);
}