On Friday, 12 July 2013 at 03:15:46 UTC, Roderick Gibson wrote:
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?
sure, each type name is unique. All you have to do is convert
them to an int... use a hash or some other method or just use the
string names. If you need the id's to be continuous try to use an
set(array) of the type names. Not sure if the last method works
well with ctfe's though.