Lambda-Over-Lambda在C ++中14 [英] Lambda-Over-Lambda in C++14
本文介绍了Lambda-Over-Lambda在C ++中14的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何递归lambda调用结束/终止?
How following recursive lambda call ends/terminates ?
#include <cstdio>
auto terminal = [](auto term) // <---------+
{ // |
return [=] (auto func) // | ???
{ // |
return terminal(func(term)); // >---------+
};
};
auto main() -> int
{
auto hello =[](auto s){ fprintf(s,"Hello\n"); return s; };
auto world =[](auto s){ fprintf(s,"World\n"); return s; };
terminal(stdout)
(hello)
(world) ;
return 0;
}
我在这里缺少什么?
推荐答案
这不是一个递归函数调用, / p>
It's not a recursive function call, look at it step-by-step:
-
terminal(stdout)
- 这只是返回一个已捕获stdout
- 使用lambda
hello
调用1.的结果, lambda(func(term)
),其结果传递到terminal()
如1中所示。 - 使用lambda
world
调用2.的结果,它与2相同,返回值被舍弃...
terminal(stdout)
- this simply returns a lambda which has capturedstdout
- The result of 1. is called with the lambda
hello
, which executes the lambda (func(term)
), the result of which is passed toterminal()
, which simply returns a lambda as in 1. - The result of 2. is called with the lambda
world
, which does the same as 2, this time the return value is discarded...
这篇关于Lambda-Over-Lambda在C ++中14的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文