Templated functions: explicit and implicit instantiation at the same time?

Don Clugston dac at nospam.com.au
Thu Nov 1 01:02:00 PDT 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) {...}
}


More information about the Digitalmars-d-learn mailing list