Bug 6312 – template instance cannot use argument from enclosing template
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-07-14T04:16:00Z
Last change time
2015-06-09T05:11:47Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
hoganmeier
Comments
Comment #0 by hoganmeier — 2011-07-14T04:16:39Z
extern(C) void printf(const char*, ...);
void h() { printf("h()"); }
class Bla
{
mixin wrap!h;
}
mixin template wrap(alias f)
{
void blub(alias g = f)()
{
g();
}
}
void main()
{
Bla b = new Bla();
b.blub();
}
$ dmd -c nonlocaltemplate.d
nonlocaltemplate.d(20): Error: template instance cannot use local 'f' as parameter to non-global template blub(alias g = f)
nonlocaltemplate.d(20): Error: template instance forward reference of f
nonlocaltemplate.d(20): Error: template instance nonlocaltemplate.Bla.wrap!(h).blub!(f) error instantiating
Note that imho this is different from http://d.puremagic.com/issues/show_bug.cgi?id=3051 because here the argument is not on the stack but it's a template argument (and thus a compile-time value)
Comment #1 by hoganmeier — 2011-11-11T08:24:41Z
Ok, here's a real ugly workaround:
extern(C) void printf(const char*, ...);
void h() { printf("h()"); }
class Bla
{
mixin wrap!h;
}
private enum dummy {foo}
mixin template wrap(alias f)
{
void blub(alias h = dummy)()
{
static if (is (h == enum))
alias f g;
else
alias h g;
g();
}
}
void main()
{
Bla b = new Bla();
b.blub();
}