Template question

Chad J "gamerChad\" at spamIsBad gmail.com
Wed Oct 4 12:52:01 PDT 2006


I'm trying to write a template that, when instantiated, gives a value 
that is the same as the last time it was instantiated plus one.  Here is 
some code I tried, but it doesn't work:

import std.stdio;

template GetUID()
{
     const ulong accumulator;

     static if ( ++accumulator != 0 )
         const ulong GetUID = accumulator;
     else
         static assert(0);
}

void main()
{
     writefln( GetUID!() ); // should print 1
     writefln( GetUID!() ); // should print 2
}

Hopefully that gives a good idea of what I am shooting for.



More information about the Digitalmars-d-learn mailing list