Variadic template arguments unpacking
Artur Skawina
art.08.09 at gmail.com
Sat Jul 6 11:26:10 PDT 2013
On 07/06/13 19:42, TommiT wrote:
> TS[N+1..$]);
> Notice that there were a couple of typos in _TypeMap.
Thanks for catching that.
> int[2] getArray(int n)
> {
> int[2] data = n;
> return data;
> }
>
> void foo(R...)(R ranges)
> {
> foreach (range; ranges)
> foreach (value; range)
> write(value, " ");
> }
>
> void main()
> {
> // Expecting this to print: 1 1 2 2 3 3
> foo(ForEach!(a => getArray(a)[])(1, 2, 3).tuple);
> // ... but it prints random garbage
> }
This is slicing a local static array and returning that slice,
which points to the stack and lives only until the lambda
returns. Moving the data to the heap or simply returning it
directly (ie by value) will work:
> foo(ForEach!(a => getArray(a).dup[])(1, 2, 3).tuple);
> foo(ForEach!(a => getArray(a))(1, 2, 3).tuple);
I like Max's solution better than mine too, btw. Why didn't i
think of that... :)
artur
More information about the Digitalmars-d-learn
mailing list