3 ms·
Generalized capture is primarily useful for move semantics: std::function<int()> make_func(std::unique_ptr<int> ptr) { return [p = std::move(ptr)]() {
by ynik 5y ago
Generalized capture is primarily useful for move semantics:
std::function<int()> make_func(std::unique_ptr<int> ptr) {
return [p = std::move(ptr)]() { return *p; }
}
Without generalized capture syntax, there's no good way to transfer ownership into the closure (a regular `[ptr]` capture would attempt to make a copy, and a by-ref `[&ptr]` capture would lead to a use-after-free).