Templates and Functions

Regan Heath regan at netmail.co.nz
Wed Sep 12 03:56:00 PDT 2007


Regan Heath wrote:
> Oliver wrote:
>> Hi,
>>
>> I'd like to hand over a function to a template. However I am unable to 
>> get access to the functions argument type. In the following code the 
>> function's argument has been set to double but it should actually be 
>> of type T2. T2 is this case is an array and i would like something to 
>> give me the type of the array which i can then use to replace the 
>> double with.
>> Any suggestions on how to do this? Thanks a lot.
>>
>> -------
>> import std.stdio;
>> import std.math;
>>
>> T1 [] dMap( T1, T2 )( T1 function(double) fp, T2 array) {
>>         T1 newArray[];
>>         newArray.length = array.length;
>>         for(int i = 0; i < array.length; i++) {
>>             newArray[i] = fp(array[i]);
>>         };
>>         return newArray;
>> }
>>
>> void main() {
>>         auto b = [0., 6.5, 3.1415];
>>         real function(double a) fp;         fp = function(double a) { 
>> return sin(a);} ;
>>         auto d = dMap(fp,b);
>>         writefln("d = ", d);
>> }
>>
> 
> I figured you'd use this:
> 
> T1 [] dMap( T1, T2:T2[] )( T1 function(T2) fp, T2[] array) {
> 
> But that gives:
> 
> template dbl.dMap(T1,T2 : T2[]) specialization not allowed for deduced 
> parameter T2
> 
> and I have no idea why specialization is not allowed :(
> 
> Regan

If you also call it with:

auto d = dMap!(real,double[])(fp,b);

it works, but that's a pain.

Regan


More information about the Digitalmars-d-learn mailing list