Bug 5390 – Make it possible to test whether a type is an instantiation of a particular template

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2010-12-29T15:10:00Z
Last change time
2016-01-26T13:10:44Z
Assigned to
nobody
Creator
issues.dlang

Comments

Comment #0 by issues.dlang — 2010-12-29T15:10:35Z
I'm marking this as dmd, because I don't think that it's currently possible to do this with the language as-is - I think that __traits needs to be updated. But if there _is_ a way to do this as-is, then it should probably be a Phobos issue for std.traits. In any case, what one thing that I'm seeing people ask for is the ability to ask in a template constraint whether a type is an instantiation of a particular template. e.g. struct A(T) { } struct B(T) if(is(T : A)) { } Of course, this particular syntax doesn't work, but it's the kind of thing that some folks are trying. They want a way to test whether T is A!(any type whatsoever). As far as I can determine, there is _no_ generic way to do this. You can do stuff like struct B(T, U) if(is(T == A!U) && isIntegral!U) { } but you generally have to find some way to either get the template constraint to give you the value for U so that you can test that T is an instantiation of A!U, or you have add some kind of flag or variable to the type that you test to see whether it exists on the type that you're testing. Ideally, you'd be able to do something like struct B(T) if(__traits(instantiatedFrom, T, A)) {} or struct B(T) if(instantiationOf(T, A)) { } There is not currently any such trait in __traits or std.traits, and as far as I can determine, one cannot be built from the current building blocks. I expect that a change will be have to made to __traits on the compiler side of things to make such a test possible. But regardless of what changes would or wouldn't have to be made to make such a test possible, _having_ such a test seems to definitely be something that some folks are looking for. I know that I would find it useful, and we really should have a straightforward way of doing such tests.
Comment #1 by andrej.mitrovich — 2010-12-29T17:44:29Z
The only thing I can think of would be using strings and mixins, but this would look really bad and would essentially be just a hack.
Comment #2 by bearophile_hugs — 2010-12-29T22:42:39Z
See also issue 5361
Comment #3 by michal.minich — 2010-12-30T12:32:30Z
This works: import std.stdio; class Dict (K, V) {} void main () { Dict!(string, int) dict; Foo (dict); } template Foo (C : T!(P1, P2), alias T, P1, P2) { void Foo (C container) { writeln(C.stringof); // Dict writeln(T.stringof); // Dict(K, V) writeln(P1.stringof); // string writeln(P2.stringof); // int } } Unfortunately it seems not to work with variadic templates, so overloads for parameter P# need to be created currently, to make it practically generic. If needed, this template can be called recursively in case P# is template instance.
Comment #4 by jakobovrum — 2016-01-26T13:10:44Z
This was fixed some time ago, it now works with variadic arguments, and there's a wrapper for it in std.traits.isInstanceOf.