Bug 11219 – isExpression should work on non-type template instantiations
Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-10T10:55:00Z
Last change time
2013-11-14T00:30:07Z
Assigned to
nobody
Creator
simen.kjaras
Comments
Comment #0 by simen.kjaras — 2013-10-10T10:55:34Z
Given a templated type:
struct Foo(int x) {
enum myValue = x;
}
I can check if something is an instantiation of this type:
template isFoo(T) {
enum isFoo = is(T == Foo!U, U...);
}
But when the template acts only as a namespace:
template Bar(T) {
enum myValue = x;
}
the isExpression recognizes Bar!T as not a type, and the comparisons return false.
A useful idiom is to store compile-time values in such a template (typetuples that don't auto-flatten, for instance), and later being able to tell if the value being passed is an instantiation of template A or template B:
template Rectangle(float w, float h) {
enum values = TypeTuple!(w,h);
}
template Circle(float radius) {
enum values = TypeTuple!(radius);
}
template doStuff(alias A) {
// Behave differently for a rectangle than for a circle.
}
Comment #1 by yebblies — 2013-11-14T00:30:07Z
This cannot work - in this code:
template Rectangle(float w, float h) {
enum values = TypeTuple!(w,h);
}
Rectangle!(1.0f, 1.0f) _is_ TypeTuple!(1.0f, 1.0f) - the wrapping template is gone once semantic is evaluated.
You're asking for something impossible - determining if a literal _could_ be produced by a template.
eg
template x(int a) { enum x = a; }
static assert(is(x!3 == x!N, N));
Compiler sees it as
static assert(is(3 == x!N, N));