Map one tuple to another Tuple of different type

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 21 12:01:27 PDT 2014


On Monday, 21 July 2014 at 18:10:14 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
> On Mon, Jul 21, 2014 at 12:55:34AM +0200, Daniel Gibson via 
> Digitalmars-d-learn wrote:
>> Hi,
>> I have a variadic templated function and want to call a C 
>> varargs function.
>> I want to be able to pass static arrays, which D2 passes by 
>> value and C by
>> reference, so I'd like to automagically translate those 
>> arguments.
>> 
>> My idea was something like this:
>> 
>>   extern (C) origFun(int x, ...);
>> 
>>   T transTupleElem(T)(T arg) { return arg; }
>> 
>>   float* transTupleElem(T : float[3])(T arg) {
>>     return arg.ptr;
>>   }
>> 
>>   void fun(T...)(int x, T argTuple) {
>>     // create a new tuple type that replaces all static 
>> float[3]
>>     // arrays with float* to emulate C call-by-reference 
>> behavior
>>     alias ReplaceAll!(float[3], float*, T) ModifiedTuple;
>>     ModifiedTuple modTuple;
>> 
>>     foreach(size_t i ; 0 .. T.length)
>>       modTuple[i] = transTupleElem(argTuple[i]); // BOOM!
>> 
>>     origFun(modTuple); // or is it modTuple.expand ?
>>   }
>> 
>> However, this doesn't work (dmd 2.065 linux64), because:
>> "Error: variable i cannot be read at compile time"
> [...]
>
> Try this:
>
> 	import std.typecons : staticIota;
> 	foreach (i; staticIota!(0, T.length))
> 		modTuple[i] = transTupleElem(argTuple[i]);
>
>
> T

staticIota is marked package in std.typecons


More information about the Digitalmars-d-learn mailing list