Thread overview
Templated functions: explicit and implicit instantiation at the same time?
Nov 01, 2007
Don Clugston
Nov 01, 2007
Don Clugston
October 31, 2007
This is driving me up the wall.  I want to have a function like this:

R func(R = int, T)(T t)
{

}

Where R can be specified but defaults to int, and T can be any type inferred from the parameter.  Of course this function signature doesn't work, and neither does anything else I've tried.  I want to be able to do:

    func(5); // R is int, T is typeof(5)
    func!(float)(5); // R is float, T is typeof(5)

The problem is that explicit instantiation disables IFTI making it impossible to derive T, and if I have multiple templates named 'func' and some of them are function templates and some aren't, it won't work with IFTI.

It seems that I can get one or the other, but not both.  Is there a solution?


November 01, 2007
Jarrett Billingsley wrote:
> This is driving me up the wall.  I want to have a function like this:
> 
> R func(R = int, T)(T t)
> {
> 
> }
> 
> Where R can be specified but defaults to int, and T can be any type inferred from the parameter.  Of course this function signature doesn't work, and neither does anything else I've tried.  I want to be able to do:
> 
>     func(5); // R is int, T is typeof(5)
>     func!(float)(5); // R is float, T is typeof(5)
> 
> The problem is that explicit instantiation disables IFTI making it impossible to derive T, and if I have multiple templates named 'func' and some of them are function templates and some aren't, it won't work with IFTI.
> 
> It seems that I can get one or the other, but not both.  Is there a solution? 
> 
> 
Do it in two levels.

template func(R=int)
{
   R func(T)(T t) {...}
}
November 01, 2007
"Don Clugston" <dac@nospam.com.au> wrote in message news:fgc15n$226b$1@digitalmars.com...
> Do it in two levels.
>
> template func(R=int)
> {
>    R func(T)(T t) {...}
> }

First thing I tried.  This works for the explicit case, but you can't call it with IFTI, since func is no longer a function template.  I wish the 'is this a function template?' check were a little more lenient/intelligent in some cases.


November 01, 2007
Jarrett Billingsley wrote:
> "Don Clugston" <dac@nospam.com.au> wrote in message news:fgc15n$226b$1@digitalmars.com...
>> Do it in two levels.
>>
>> template func(R=int)
>> {
>>    R func(T)(T t) {...}
>> }
> 
> First thing I tried.  This works for the explicit case, but you can't call it with IFTI, since func is no longer a function template.  I wish the 'is this a function template?' check were a little more lenient/intelligent in some cases. 
> 
> 
Oh, yeah. Now I remember. You still have to put in a !(). Bummer.
func!(long)(3.0i);
func!()(4.0);

That was the best I could find, around DMD 1.0.