Compile-Time std::map
Jari-Matti Mäkelä
jmjmak at utu.fi.invalid
Fri Apr 27 16:34:07 PDT 2007
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.
More information about the Digitalmars-d
mailing list