October 10, 2013 Re: How to check for instantiation of specific template? | ||||
---|---|---|---|---|
| ||||
On 2013-10-10, 19:23, H. S. Teoh wrote: > I have a template used for storing compile-time values: > > template Def(int x, string y) { > alias impl = TypeTuple!(x,y); > } > > How do I define a template isDef that, given some template alias A, > evaluates to true if A is some instantiation of Def? > > template isDef(alias A) { > enum A = ... /* what to put here? */ > } This seems to be impossible. The standard way of doing this would be with an isExpression[0], but that only works for types. A solution would be to beef up your Def template by turning it into a struct: struct Def(int x, string y) { alias impl = TypeTuple!(x,y); } With this definition, the following works: template isDef(alias A) { enum isDef = is(A == Def!T, T...); // Or enum isDef = is(A == Def!(x, y), int x, string y); } This is a shortcoming of the language, and I have filed an enhancement request for it[1]. The problems of using an actual type for this, is that someone might instantiate that type (unlikely), and that the compiler will generate TypeInfo for it, which will bloat your executable. A Sufficienctly Smart Compiler[2] would see that the type is never used and thus not include the TypeInfo. Something which the compiler could do without a lot of smarts, is optimize away final abstract classes, which you cannot derive from and cannot instantiate. That oughta be enough. [0]: http://dlang.org/expression#IsExpression [1]: http://d.puremagic.com/issues/show_bug.cgi?id=11219 [2]: http://c2.com/cgi/wiki?SufficientlySmartCompiler -- Simen |
Copyright © 1999-2021 by the D Language Foundation