News for hackers. Stuff that matters.
Re:Zero-cost futures in Rust by astn on Thursday August 11, 2016 @01:19PM (#12270040)
In C++ lambdas have limited size (about 3 machine words). If it's context is larger than that, the data may be allocated on heap (if compiler can't optimize that allocation out).
Re:Zero-cost futures in Rust by comex on Thursday August 11, 2016 @02:05PM (#12270407)
What you're talking about is std::function, a wrapper around raw lambda objects that performs dynamic dispatch. Since any lambda can be wrapped in std::function yet std::function, like all C++ types, must have a fixed size, it uses the heap as you say.

If you stick to the raw lambdas, however, the compiler knows the size and doesn't need to use the heap. Only problem is that their type can't be named. In C++, you can work around this with auto.

Without getting too much into it, Rust has a similar distinction between raw lambdas and Box<Fn(...)>. This library uses the former.

Re:Zero-cost futures in Rust by soulbadguy on Thursday August 11, 2016 @01:56PM (#12270343)
I though in c++ the lambda get created on the stack like any other object.
Re:Zero-cost futures in Rust by astn on Thursday August 11, 2016 @02:56PM (#12270751)
They do, but to actually use it, to pass it anywhere, you have to either wrap it in some other object (std::function, as comex pointed out) or use a template.