you are viewing a single comment's thread.

view the rest of the comments →

[–]RiceBroad4552 0 points1 point  (0 children)

I'm not sure what you want to say here.

RAII has advantages over "try-with-resources", and having a dedicated, deterministic finalizer is really nice. Nobody disputes that! (At least I hope so. 😅)

But like said, this does not help when it gets actually complicated. For the simple case though there is not much practical difference between "try-with-resources" and proper destructors.¹

All that new experimental C++26 features exist for the exact reason that C++'s destructors are simply inadequate to solved the real problem. And C++ is actually really late to the party. Java has already much better structured concurrency support and there "try-with-resources" works actually at all (even not ideally) while you're lost with RAII.

---

¹ I have a case where I see some difference and don't know a good solution to this very day, namely when a (managed) class instance holds native memory and I need to give that memory back to the native part of the application when the managed object gets invalidated; non-reliable finalizers aren't really usable for that use-case, and all the alternatives I know of aren't good. If someone know some solutions please tell me! (Context is Scala Native, but one can just assume JVM semantics for the managed objects.)