www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Templated functions: explicit and implicit instantiation at the same time?

reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
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? 
Oct 30 2007
parent reply Don Clugston <dac nospam.com.au> writes:
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? 
 
 

template func(R=int) { R func(T)(T t) {...} }
Nov 01 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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.
Nov 01 2007
parent Don Clugston <dac nospam.com.au> writes:
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.

func!(long)(3.0i); func!()(4.0); That was the best I could find, around DMD 1.0.
Nov 01 2007