auto scope problem

Koroskin Denis 2korden+digitalmars at gmail.com
Tue Dec 4 05:11:31 PST 2007


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
...

however, it doesn't work in loops:

for(auto Iterator it = myVec.begin(); it.hasNext(); it.moveNext())
{
   // do something
}

An object is deleted right after first check (iterator.hasNext()).
It doesn't work in both 1.x and 2.x compiler branches.
Is this behaviour ok or not?

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


More information about the Digitalmars-d-bugs mailing list