Set*_*eth 5 c++ lambda function c++11
我最近偶然发现了这个示例代码,我很困惑:
auto named_funct(const MyClass& some_class)
{
return [some_class](const MyClass2& some_other_class)
{
return some_class <= some_other_class; // some expression between capture and input parameter to the Lambda
};
}
Run Code Online (Sandbox Code Playgroud)
是否有任何理由将匿名函数包装在命名函数中?似乎无缘无故地创建了一个额外的函数调用堆栈。
named_funct不执行 lambda,而是返回它。你可以这样使用它:
auto it = std::find_if(std::begin(some_vector), std::end(some_vector),
named_funct(some_instance));
Run Code Online (Sandbox Code Playgroud)
奇怪的是,named_funct它接受的参数不会做任何事情,除非出现复制/粘贴错误(另一个实例被传递给实际的 lambda)。这个例子也很微不足道,我看不到 lambda 的好处,但如果named_funct的参数被捕获并以某种方式使用,这是一个有用的模式。
| 归档时间: |
|
| 查看次数: |
79 次 |
| 最近记录: |