Can I use a delegate as a template parameter? (Issue 2962?)

Magnus Lie Hetland magnus at hetland.org
Wed Mar 30 05:07:51 PDT 2011


A while ago, I wrote something like this:

void C(alias foo)() {
    void bar() {
        foo();
    }
    bar();
}

void B(T)(uint x=2) {
    uint foo() {
        return x;
    }
    C!foo();
}

It worked well, so I didn't think any more about it. Then suddenly, I 
started getting the following error:

./path/to/foo.d(12): Error: function path.to.foo.B!(real).B compiler 
error, parameter 'x', bugzilla 2962?
Assertion failed: (0), function toObjFile, file glue.c, line 729.

This only happens when I compile with -inline, and error disappears if 
I make certain really minor changes to which modules are imported where 
(currently 17 basically empty modules are involved in the minimal case 
I've been able to produce), so the compiler behavior seems like a bug.

As Don says in a comment to 2962, "This is really tough, it's an 
order-of-evaluation issue.
When generating the code for a template, which has a local variable as an alias
parameter, the alias parameter MUST be created before the code for 
template is."

The error message also seems different from that of issue 2962 (which 
DMD dutifully points me toward :), so I'm uncertain about whether it's 
the same issue. What does it look like to you guys?

Also, aside from the erratic DMD behavior, I'm guessing my code might 
be invalid, and shouldn't have worked in the first place. After all, x 
is a run-time argument, and is part of foo, which functions as a 
compile-time argument. I mean, the set-up *could* work (and, in fact, 
generally seems to do so), but it might be a bit problematic?

This way of doing it seems like the most practical for my current use, 
though... (I.e., I'd like foo to be inline-able, and have access to x.)

-- 
Magnus Lie Hetland
http://hetland.org



More information about the Digitalmars-d-learn mailing list