Bug 3538 – Default value of alias template parameter is instantiated only once.
Status
RESOLVED
Resolution
DUPLICATE
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2009-11-21T12:28:04Z
Last change time
2022-01-28T09:52:49Z
Keywords
patch, wrong-code
Assigned to
No Owner
Creator
Eldar Insafutdinov
Comments
Comment #0 by e.insafutdinov — 2009-11-21T12:28:04Z
template Boo(T) {}
struct Foo(T, alias V = Boo!T) { pragma(msg, V.stringof); }
alias Foo!double B;
alias Foo!int A;
outputs
Boo!(double)
Boo!(double)
while it should
Boo!(double)
Boo!(int)
Although it's a blocker for a design that I intend to use, I don't mark it as such with hope that it'll get fixed, as it looks trivial to me.
Comment #1 by robert — 2010-04-23T12:50:46Z
This is caused as default arguments to templates are only instantiated once, which causes the Boo!T to always become whatever is instantiated first. The patch below fixes this:
--- template.c 2010-03-18 18:58:06.000000000 +0000
+++ template.c 2010-04-23 20:49:54.000000000 +0100
@@ -2993,6 +2993,17 @@
Object *TemplateAliasParameter::defaultArg(Loc loc, Scope *sc)
{
+ Type *ta = isType(defaultAlias);
+ if (ta)
+ {
+ if (ta->ty == Tinstance)
+ {
+ // If the default arg is a template, instantiate for each type
+ Object *da = ta->syntaxCopy();
+ Object *o = aliasParameterSemantic(loc, sc, da);
+ return o;
+ }
+ }
Object *o = aliasParameterSemantic(loc, sc, defaultAlias);
return o;
}
Comment #2 by bugzilla — 2010-05-16T11:15:20Z
changelog 492
Comment #3 by alexander.breckel — 2016-02-01T13:07:41Z
This problem still exists, if the default expression is not a type, but an actual expression referring to a type:
template Tpl(T, alias S = "" ~ T.stringof) {
pragma(msg, S);
}
class A { }
class B { }
alias TA = Tpl!A;
alias TB = Tpl!B;
I think a possible solution would be to always use a syntaxCopy() of defaultAlias. How would this affect performance?
Comment #4 by razvan.nitu1305 — 2022-01-28T09:52:49Z
*** This issue has been marked as a duplicate of issue 22540 ***