digitalmars.D - auto classes
- Sark7 (25/25) Jun 15 2004 At now auto classes must be declared explicitly to get RAII to work.
- Arcane Jill (5/28) Jun 16 2004 Yeah, that sounds like a bug to me. With a slight modification to the co...
- =?ISO-8859-1?Q?Sigbj=F8rn_Lund_Olsen?= (9/51) Jun 16 2004 " When an auto class reference goes out of scope, the destructor (if
At now auto classes must be declared explicitly to get RAII to work. But implicit declaration can be very useful, like following: [code] auto class A { ~this() { printf("A dtor"\n); } } void foo(A a) {} void bar() { foo(new A()); printf("A out of the scope"\n); } void main() { bar(); } [/code] Currently I must to declare `auto A a = new A()` in bar() and pass `a` to foo(), but it is not good enough for me :) Besides, default behaviuor seems like a bug, because `new A()` passed to foo() in that case is not auto reference; and collected as non-auto class.
Jun 15 2004
In article <opr9nrzfikut8jae zaitseff.tec.amursk.ru>, Sark7 says...Besides, default behaviuor seems like a bug, because `new A()` passed to foo() in that case is not auto reference; and collected as non-auto class.Yeah, that sounds like a bug to me. With a slight modification to the code, this bug becomes stunningly obvious:auto class A { ~this() { printf("A dtor"\n); } } void foo(A a) {} void bar() { foo(new A()); printf("A out of the scope"\n); } void main() { bar(); printf("For correct RAII, A should have been deleted BEFORE this point\n"); }Prints:A out of the scope For correct RAII, A should have been deleted BEFORE this point A dtorArcane Jill
Jun 16 2004
Arcane Jill wrote:In article <opr9nrzfikut8jae zaitseff.tec.amursk.ru>, Sark7 says..." When an auto class reference goes out of scope, the destructor (if any) for it is automatically called. This holds true even if the scope was exited via a thrown exception." This really is a showstopping bug, as often you absolutely *must* be sure that some resource is released before running subsequent code. *nudges Walter* Cheers, Sigbjørn Lund OlsenBesides, default behaviuor seems like a bug, because `new A()` passed to foo() in that case is not auto reference; and collected as non-auto class.Yeah, that sounds like a bug to me. With a slight modification to the code, this bug becomes stunningly obvious:auto class A { ~this() { printf("A dtor"\n); } } void foo(A a) {} void bar() { foo(new A()); printf("A out of the scope"\n); } void main() { bar(); printf("For correct RAII, A should have been deleted BEFORE this point\n"); }Prints:A out of the scope For correct RAII, A should have been deleted BEFORE this point A dtorArcane Jill
Jun 16 2004