Boost Lambda / Phoenix - how to make a lambda that returns another lambda?

Does Boost Lambda / Phoenix support out of the box something like a lambda that returns another lambda?

For example, this can be used to do some kind of currying:

std::cout << [](int x){return [=](int y){return x+y;};}(1)(2); 

How to achieve a similar goal with Boost Lambda / Phoenix (+ as a bonus - we get polymorphic behavior)?

+6
source share
1 answer

Boost Phoenix Scope: let / lambda

Demo version:

 #include <boost/phoenix.hpp> #include <iostream> #include <ostream> using namespace std; using namespace boost; using namespace phoenix; using namespace arg_names; using namespace local_names; int main() { // capture by reference: cout << (lambda(_a=_1)[_1 + _a ])(1)(2) << endl; cout << (lambda(_b=_1)[lambda(_a=_1)[_1 + _a + _b ]])(1)(2)(3) << endl; // capture by value: cout << (lambda(_a=val(_1))[_1 + _a ])(1)(2) << endl; cout << (lambda(_b=val(_1))[lambda(_a=val(_1))[_1 + _a + _b ]])(1)(2)(3) << endl; } 

Output:

 3 6 3 6 
+5
source

All Articles