What is this C++14 construct called which seems to chain lambdas?

后端 未结 3 918
一个人的身影
一个人的身影 2021-02-14 01:47

This is a follow-up question on this one: Lambda-Over-Lambda in C++14, where the answers explain the code.

It is about a lambda that creates another lambda which when ca

3条回答
  •  野性不改
    2021-02-14 02:05

    terminal(x) returns an applicator that method-chains its return value into terminal for repeated invocation.

    But we could instead generalize it.

    Suppose you have a function F. F takes an argument, and stuffs it on a stack.

    It then examines the stack. If the top of the stack, evaluated on some subset of the stack, would work for invocation, it does it, and pushes the result back onto the stack. In general, such invocation could return a tuple of results.

    So:

    F(3)(2)(add)(2)(subtract)(7)(3)(multiply)(power)
    

    would evaluate to:

    ((3+2)-2)^(7*3)
    

    Your terminal does this with 0 argument functions (the first argument) and with 1 argument functions (every argument after that), and only supports 1 return value per invocation.

    Doing this with a lambda would be tricky, but what I described is doable in C++.

    So one name for it would be stack-based programming.

提交回复
热议问题