[Issue 1084] lazy variadic parameters break in strange way

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 28 12:57:20 PDT 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1084





------- Comment #1 from fvbommel at wxs.nl  2007-03-28 14:57 -------
What seems to be happening here:
The parameter list of test2 is "lazy char[][] bla ...".
The type of the parameter is an array of char[]s. Because of the "..." this
array is implicitly constructed on the stack when the function is called.
However, because of the "lazy" this implicitly construction is put into a
delegate that is then pushed onto the stack as the actual parameter.
The problem with this is that the delegate constructs an array on the stack and
then returns _that_ array.
So the "lazy" delegate is returning a reference to data on the stack (in it's
own stack-frame). This is always a Bad Idea.

(I guess combining "lazy" and "..." is not a good idea)

What you may have meant was:
---
/// A call constructs an array on the stack and pass a reference.
/// Passed "parameters" are evaluated before calling.
void test2(char[][] bla ...) {
        foreach (elem; bla) writefln(elem);
}
---
or:
---
/// A call constructs an array of delegates on the stack and pass a reference
to that.
/// Passed "parameters" aren't evaluated until used.
void test2(char[] delegate()[] bla ...) {
        foreach (elem; bla) writefln(elem());
}
---


-- 



More information about the Digitalmars-d-bugs mailing list