Compile-Time std::map

NN nn-mail at bk.ru
Sat Apr 28 05:17:16 PDT 2007


Jari-Matti Mäkelä Wrote:

> NN wrote:
> > 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]);
> >> 
> 
> > But it does not do what I want.
> > I want to rearange the values in compile time.
> 
> Oh, sorry. If you just want to rearrange the array contents on compile time,
> you can do it with a CTFE function or with tuples, then assign the
> resulting tuple into an array.

OK.
If i would write:
int[int] a = f([2:2, 1:1, 3:3]);

Won't it create [2:2, 1:1, 3:3] and [1:1, 2:2, 3:3] ?
Or the compiler will remove the first array ?



More information about the Digitalmars-d mailing list