4 ms·
I think some languages let you choose to capture variables or values - does C++ do that?
by chrisseaton 7y ago
I think some languages let you choose to capture variables or values - does C++ do that?
- FreeFull 7y agoC++ and Rust do let you capture by value. Here is a Rust example that outputs exactly what you would naively expect. fn main() { let mut x = 1; let f = move || println!("{}", x); x = 2; let g = move || println!("{}", x); f(); g(); }
- Gibbon1 7y agoSort of related with gcc's nested function extension, you can reference variables that in scope when the function is defined. If you pass a pointer to the nested function is can access those variables (via a trampoline on the stack). To me that's been very useful to have both to get around not being able to use malloc. Meaning I think it's a good idea actually.