Lambda-Over-Lambda在C ++中14 [英] Lambda-Over-Lambda in C++14

查看:171
本文介绍了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:


  1. terminal(stdout) - 这只是返回一个已捕获 stdout

  2. 使用lambda hello 调用1.的结果, lambda( func(term)),其结果传递到 terminal()如1中所示。

  3. 使用lambda world 调用2.的结果,它与2相同,返回值被舍弃...

  1. terminal(stdout) - this simply returns a lambda which has captured stdout
  2. The result of 1. is called with the lambda hello, which executes the lambda (func(term)), the result of which is passed to terminal(), which simply returns a lambda as in 1.
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆