How to expand an expression along with a parameter tuple?
Ali Çehreli
acehreli at yahoo.com
Mon Jun 17 00:20:22 PDT 2013
On 06/16/2013 11:19 PM, TommiT wrote:
> I can't figure out how to do the following C++ code in D:
>
> int arr[] = { 1, 3, 5, 7, 11 };
>
> template <typename... T>
> void foo(T... values) { }
>
> template <typename... T>
> void bar(T... values)
> {
> foo((arr[values] * 10)...);
> }
>
> int main()
> {
> bar(1, 3, 4); /* calls foo(arr[1] * 10,
> arr[3] * 10,
> arr[4] * 10); */
> return 0;
> }
The following does not answer the question of expanding but at least
foo() receives [30, 70, 110] :)
import std.stdio;
import std.algorithm;
import std.array;
import std.range;
int[] arr = [ 1, 3, 5, 7, 11 ];
void foo(T)(T[] values...)
{
writeln(values);
}
void bar(T)(T[] values...)
{
foo(arr
.indexed(values)
.map!(a => a * 10)
.array);
}
void main()
{
bar(1, 3, 4);
}
Ali
More information about the Digitalmars-d-learn
mailing list