auto, var, raii,scope, banana

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Sat Jul 29 14:13:46 PDT 2006


Chad J wrote:
> Regan Heath wrote:
> 
>> Add the new RAII syntax  and not only does it not crash, but it 
>> behaves just like a C++ program  would with A being destroyed at the 
>> end of scope.
> 
> I hear that such syntax, in C++, means that 'A' will be stack allocated,
> which AFAIK does not imply RAII.  One example I can think of that would
> break RAII is if the function gets inlined, then the stack allocated
> object may not be deleted until some point outside of the scope's end.
> It will /probably/ be deleted at the end of the function that the
> enclosing function was inlined into.  I could be wrong about that though.
> 

Uh, scopes and stack "frames" do not exist only for function frames.
They exist for any instruction-block/scope. Such that:

   void func() {
     auto Foo fooa = new Foo;
     {
       auto Foo foob = new Foo;
     } // foob gets destroyed here
     ...
   } // fooa gets destroyed here

So similarly, function inlining creates a scope/instruction-block , so
that allocation behavior is preserved. So this code:

   int a = 2, b = 3;
   int r = sum(a, b);

gets inlined to this:

   int a = 2, b = 3;
   int r;
   {
     r = a + b;
   } // De-alloc autos if there were any.


-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D




More information about the Digitalmars-d mailing list