Unique IDs for each template instantiation at compile-time?

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Dec 18 16:38:09 PST 2013


On Thu, Dec 19, 2013 at 12:01:03AM +0100, Weasel wrote:
> I was wondering if it was possible to generate unique(in order) IDs
> for each template instantiation of a class at compile-time.
[...]

You could use the built-in .mangleof property to get the mangled name of
the template, which can then be used as a unique string for that
particular template instantiation.

The disadvantage is that this is a string, which may be inefficient
depending on what you want to do with it.  So you may want to take
advantage of CTFE by using the hash of this string instead, say using
the hashOf function, and use the resulting hash value as your ID.
Assuming that the hash function isn't so horrible that two template
instantiations' hash values will collide, this ID should be unique.


T

-- 
Famous last words: I *think* this will work...


More information about the Digitalmars-d-learn mailing list