auto scope problem

Jarrett Billingsley kb3ctd2 at yahoo.com
Tue Dec 4 05:30:40 PST 2007


"Koroskin Denis" <2korden+digitalmars at gmail.com> wrote in message 
news:fj3jm3$5n4$1 at digitalmars.com...
>I don't know whether it is a bug or is already known, but I haven't seen 
>the discussion around here.
>
> This code works just as intended:
>
> ...
> {
>  auto x = new SomeClass();
> }
> // x is deleted here
> ...

What compiler are you using, 0.173?  The 'auto' keyword was replaced with 
the 'scope' keyword to perform RAII more than a year ago.  I take it your 
full code looks like:

void main()
{
    auto x = new SomeClass();
}

In which case, yes, x will be deleted after main returns but only because 
the GC is run at program termination.  The 'auto' here is just triggering 
variable type inference and nothing more.  To get RAII, use 'scope':

scope x = new SomeClass();

> My second question is actually a feature request (to Walter).
> I'd like to have a struct with trivial constructor and destructor.
> C# can have one if it does nothing but initializes/deletes variables.
>
> Like this:
>
> struct VectorInt
> {
>   int size = 8;
>   auto int[] array = new int[8];
> };
>
> It is ok to have such struct and it initializes all the variables. 
> However, it lacks trivial destructor - the one that would delete an array 
> upon going out of scope (destruction).
>
> Can we have one so it would delete objects, marked as "auto", please?
> This is needed in order to have an ability to create containers on stack 
> (without initialization):
>
> {
>    /* auto */ vector!(int) myVec;  // initialized and has a capacity == 8
> } // vector is going out of scope here, and an array is destroyed 
> correctly

People have asked for RAII to work for class/struct members too.  Who knows 
if we'll get it.  Walter has a way of shooting down features and then 
implementing them out of the blue a year later. 




More information about the Digitalmars-d-bugs mailing list