Template unique IDs per type

Roderick Gibson kniteli at gmail.com
Thu Jul 11 20:15:43 PDT 2013


I was recently looking up how to assign a unique ID based on each
different implementation of a templated type (NOT per instance,
but per unique type). For example:

class MyTemplate(T) {
      //the ? where the actual number would go
      const int type_id = ?;
}

void main() {
      auto a = new MyTemplate!(int)(); //a.type_id = 0
      auto b = new MyTemplate!(string)(); //b.type = 1
      auto c = new MyTemplate!(int)(); //c.type_id = 0
      auto d = new MyTemplate!(double)();//d.type_id = 2
}

There's some solutions to this sort of thing at this
stackoverflow question in c++ which are possible at run-time:

http://stackoverflow.com/questions/8596490/counting-with-template-metaprogramming

BUT I was wondering with CTFE and/or mixins would it be possible
to do this at compile time?


More information about the Digitalmars-d-learn mailing list