From 3be48af7d588d8642accf2eb1d5dac3adf07e5a0 Mon Sep 17 00:00:00 2001 From: Brad Anderson Date: Thu, 21 Aug 2014 12:07:12 -0600 Subject: [PATCH 1/2] Don't use deprecated scope feature in guide --- cpptod.dd | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/cpptod.dd b/cpptod.dd index a56c3793af..1ef7e8f090 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,7 +448,8 @@ 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 From 9e5e34f6be3189dc592cbc6347e8636ef9c08a3f Mon Sep 17 00:00:00 2001 From: Brad Anderson Date: Thu, 21 Aug 2014 14:50:09 -0600 Subject: [PATCH 2/2] Mention scoped and Scope Guards in RAII section --- cpptod.dd | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cpptod.dd b/cpptod.dd index 1ef7e8f090..5fc5122edb 100644 --- a/cpptod.dd +++ b/cpptod.dd @@ -456,6 +456,16 @@ void test() } ------ +$(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.) + +
$(H3 Properties)