How to specify a template that uses unqualified type, like any normal function

Dominikus Dittes Scherkl via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 14 10:43:44 PDT 2017


On Monday, 14 August 2017 at 15:20:28 UTC, Steven Schveighoffer 
wrote:
> On 8/14/17 9:48 AM, Dominikus Dittes Scherkl wrote:
> > uint foo(T)(Unqual!T n) // first try
> > {
> >     ++n; // modify should be possible
> >     return 42;
> > }
> > Any ideas what I need to do to make this work?
>
> This isn't exactly supported. Implicit Function Template 
> Instantiation (IFTI) will deduce the parameters to be the types 
> that you pass in. You can't deduce them and then change the 
> parameter types. This is a limitation of IFTI that I have 
> struggled with in the past.
A little unfortunate, because I would consider this the standard 
usecase.
You only overload functions if they do something special with
const or shared or immutable parameters, but for templates you 
get a different implementation for these all the time, and you 
didn't even have a chance to avoid that useless code-bloat? What 
a pitty.

>
> What you can do, is:
>
> auto foo(T)(T n) if (is(T == Unqual!T))
> {
>    // normal implementation
> }
>
> auto foo(T)(T n) if (!is(T == Unqual!T) && 
> isImplicitlyConvertible!(T, Unqual!T))
> {
>    return foo!(Unqual!T)(n);
> }

Ok, I'll try that out.


More information about the Digitalmars-d-learn mailing list