3 ms·
That’s the point I’m trying to make: RAII is more flexible but requires more code than a defer or finally statement.
by stingraycharles 4y ago
That’s the point I’m trying to make: RAII is more flexible but requires more code than a defer or finally statement.
- IshKebab 4y agoBarely any more. It can be as simple as auto sg = scope_guard([&]() { delete whatever; }); E.g. see this implementation: https://ricab.github.io/scope_guard/ https://ricab.github.io/scope_guard/ But also I should make the point that you generally don't need to write defer-like statements at all when using RAII because it's implemented in the actual type. So it's only more code overall for types that are used only once or maybe twice.
- metadat 4y agoDo you really consider this simple, or is it sarcasm? Simple compared to what?
- IshKebab 4y agoIt is simple. At least if you know C++. I can see why it might look like it has a load of random brackets if you aren't a C++ programmer. But anyway if you're doing C++ right you would rarely need to actually do that whereas you need `defer` in Go all the time.
- pjmlp 4y agoIf that is too much typing, a macro can help.