How to specify a template that uses unqualified type, like any normal function
    ag0aep6g via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Mon Aug 14 15:38:19 PDT 2017
    
    
  
On 08/15/2017 12:14 AM, Dominikus Dittes Scherkl wrote:
> T foo(T)(T n)
> {
>     static if(!is(Unqual!T == T)) return foo!(Unqual!T)(n);
>     else
>     {
>         // normal implementation
>     }
> }
> 
> So it's basically 2 lines of overhead. That's acceptable. The check for 
> isImplicitlyConvertible is not necessary, because if it's not it will 
> error out anyway.
I'm probably missing something, but copying the parameter to a local 
variable seems simpler than doing it with overloads:
----
T foo(T)(T n)
{
     Unqual!T m = n;
     ++m;
     return m;
}
----
> As this in fact leads to some decrease in code size (as the instances 
> for const or shared parameters are now much smaller or in fact 
> completely removed by the inliner, much less template code duplication 
> happens), I will add this to a lot of templates - seems this will become 
> some sort of standard D boilerplate code.
Regarding shared, be aware that dmd lets you copy a value type from 
shared to unshared, but it doesn't emit an atomic load for the copy. So 
it's generally not thread-safe to do it.
    
    
More information about the Digitalmars-d-learn
mailing list