run-time stack-based allocation

Arne arne at linux.nu
Mon May 7 09:52:16 PDT 2012


On Monday, 7 May 2012 at 16:03:15 UTC, Mehrdad wrote:
> On Monday, 7 May 2012 at 13:36:02 UTC, Gor Gyolchanyan wrote:
>> Basically I want what alloca does, but instead of considering 
>> the constructor's scope, I want it to hand to the constructor 
>> call's enclosing scope.
>
> I think you'd need to modify the compiler for this, since 
> alloca is 'magical'.

wouldn't mixin's be a solution, one can inject an alloca to the 
current scope, and then call the constructor...

import std.stdio;
import std.c.stdlib;
import std.random;

mixin template Init(alias var, alias size)
{
   void* buf = alloca(size);

   bool foo_init()
   {
     var = (cast(typeof(var))buf);

     var[0]=0;
     var[1]=1;
     var[2]=2;

     return true;
   }

   auto foo_dummy_statement = foo_init();
}

void main()
{
   int my_size = uniform(12, 24);
   int* my_var = void; mixin Init!(my_var, my_size);

   writeln(my_var[0..3]);
}


More information about the Digitalmars-d mailing list