Indexing an associative array with a list of types

Stewart Gordon smjg_1998 at yahoo.com
Sun Apr 12 16:27:35 PDT 2009


Doctor J wrote:
<snip>
> Correct.  At compile time, I want to build up an associative array
> mapping type tuples to integers.  Then, again at compile time, I want
> to query it.

How about this?

----------
import std.typetuple, std.stdio;

template typesToInt(T1 : int, T2 : float) {
     const int typesToInt = 1;
}

template typesToInt(T1 : long, T2 : double) {
     const int typesToInt = 2;
}

template intToTypes(int T : 1) {
     alias TypeTuple!(int, float) intToTypes;
}

template intToTypes(int T : 2) {
     alias TypeTuple!(long, double) intToTypes;
}

// testing code from this point forward

void listTypes(T...)() {
     writefln();
     foreach (type; T) writefln(typeid(type));
}

void main() {
     writefln(typesToInt!(int, float));
     writefln(typesToInt!(long, double));

     listTypes!(intToTypes!(1))();
     listTypes!(intToTypes!(2))();
}
----------

HTH

Stewart.


More information about the Digitalmars-d-learn mailing list