5 ms·
> closures both create problems for RAII and also obviate many common use cases. As someone who uses a lot of closures and a lot of RAII, together, I can assur
by kentonv 11y ago
> closures both create problems for RAII and also obviate many common use cases.
As someone who uses a lot of closures and a lot of RAII, together, I can assure you this is not the case:
- C++ closures are very explicit about what is captured and how (by reference, by copy, by move/ownership transfer). It actually works incredibly well with RAII.
- Closures as an alternative to RAII only works in a small subset of use cases: cases where the resource lifetime is bound to a stack frame. They do not easily model resources owned by objects, nor (ironically) resources used in an asynchronous callback context.
BTW, Rust actually statically guarantees no dangling pointers -- without using GC. And yes, it has closures.