www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - auto classes

reply Sark7 <sark7 mail333.com> writes:
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
parent reply Arcane Jill <Arcane_member pathlink.com> writes:
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 dtor
Arcane Jill
Jun 16 2004
parent =?ISO-8859-1?Q?Sigbj=F8rn_Lund_Olsen?= <sigbjorn lundolsen.net> writes:
Arcane Jill wrote:
 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 dtor
Arcane Jill
" 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 Olsen
Jun 16 2004