C– Lambda++
8, Dec, 2018In the meantime, I am in lisp/scheme thing. The functional-like structuring manner, can be quite enlightening to day-to-day C tasks. Just simple closure passing in scheme:
(define foo (lambda (x) (lambda (y) (+ x y)))) (define bar (lambda (f i) (f i))) (bar (foo 3) 5)
And then I got the “wonderful” idea. Lets try this in C++:
auto foo(int x) {
return [x](int y) { return x+y; };
}
int bar(???? f, int i) {
return f(i);
}
int main(int argc, const char * argv[]) {
printf("%dn", bar(foo(5)), 3);
return 0;
}
and when I simply wanted to pass lambda created by foo to function bar, I got a moment of WHAAAT!? Is the std::function the only option to pass lambda with captures (you cannot pass lambda with capture by cast to function pointer). This is ridiculous.
- Posted by michal in
- Add a comment