More range woes: composed ranges are unsafe to return from functions
jerro
a at a.com
Tue Oct 16 13:28:52 PDT 2012
On Tuesday, 16 October 2012 at 19:31:18 UTC, H. S. Teoh wrote:
> On Tue, Oct 16, 2012 at 08:44:56PM +0200, monarch_dodra wrote:
> [...]
>> No idea either, but I'll volunteer to investigate ;)
>>
>> I agree with Jonathan though, sounds like a bug somewhere.
>> There
>> is no reason for your code to fail.
>>
>> I'll post back if I find anything.
>
> Another data point: if I move the .joiner call out of cprod()
> into
> main(), then there is no problem. Could it be that something in
> joiner
> is breaking somehow, when returned from a function?
>
>
> T
I don't know, but if I change the code to this:
auto cprod(R1,R2)(R1 A, R2 B) {
// This part is exactly the same as in main(), below. So
// in theory, it should work exactly the same way.
auto mapper = (typeof(tuple(cast(size_t) 0, A.front,
B.front)) a) => chain(
zip(repeat(a[1]), B.save.take(a[0])),
zip(A.save.take(a[0]+1), repeat(a[2])));
auto r = zip(sequence!"n"(cast(size_t)0), A.save, B.save)
.map!mapper()
.joiner;
// But something goes wrong here: is it because the
// above composed ranges are stack-allocated temporaries
// that go out of scope upon return?
return r;
}
It works too.
More information about the Digitalmars-d
mailing list