diff --git a/cpptod.dd b/cpptod.dd index a56c3793af..5fc5122edb 100644 --- a/cpptod.dd +++ b/cpptod.dd @@ -431,12 +431,13 @@ $(H4 The D Way) and locks, handled automatically with D's $(D synchronized) declarations and statements.) - $(P The few RAII issues left are handled by $(I scope) classes. - Scope classes get their destructors run when they go out of scope.) + $(P The few RAII issues left are handled by $(D struct)s. + A $(D struct) gets its destructor run when it goes out of scope.) ------ -scope class File -{ Handle h; +struct File +{ + Handle h; ~this() { @@ -447,13 +448,24 @@ scope class File void test() { if (...) - { scope f = new File(); + { + auto f = File(); ... } // f.~this() gets run at closing brace, even if // scope was exited via a thrown exception } ------ +$(P $(D class)es are typically managed by the garbage collector which doesn't +lend itself to RAII. If you need deterministic destruction with $(D class)es +you can use $(FULL_XREF typecons, scoped) (which will also allocate the +$(D class) on the stack instead of the garbage collector managed heap).) + +$(P See also $(GLINK2 statement, ScopeGuardStatement) for a more generalized +mechanism that lets you run arbitrary statements whenever leaving the current +scope.) + +