Compile-Time std::map

NN nn-mail at bk.ru
Sat Apr 28 04:24:44 PDT 2007


Jari-Matti Mäkelä Wrote:

> NN wrote:
> > I allways wanted to create std::map but in compile time.
> > 
> > The problem:
> > I want to write smth like:
> > int[int] x = {3:3,2:2,1:1}
> > 
> > But compiler will generate the following:
> > int[int] x = {1:1,2:2,3:3}
> > 
> > Thus I can use binary search on x.
> > 
> > Is it possible to do in D ?
> > Thanx.
> 
> Well, in case you didn't notice, DMD 1.014 now includes hash literals.
> Assigning static hashes aren't yet supported directly, but you can do
> simple cases like
> 
>   int val = ([1:11,2:22,3:33][1]);
> 
> Why does it need extra parenthesis - don't ask me.
> 
> Don and BCS showed how you can simulate assignment to a static variable
> with
> 
>   char[][uint] symTable() { return [2u:"he",4:"ho",6:"hi"]; }
> 
> and
> 
>   char[][uint] symTable;
>   static this() { symTable=[2u:"he",4:"ho",6:"hi"]; }
> 
> They both have some limitations.
But it does not do what I want.
I want to rearange the values in compile time.



More information about the Digitalmars-d mailing list