How to automatically generate function overloads

Blatnik blatblatnik at gmail.com
Tue May 4 11:45:35 UTC 2021


On Tuesday, 4 May 2021 at 11:21:20 UTC, Zone wrote:
> ```D
> template Vectorize_Unary_Function(alias fun) {
>     float[N] Vectorize_Unary_Function(size_t N)(float[N] vec)
>     {
>         float[N] result;
>         static foreach (i; 0 .. N)
>             result[i] = fun(vec[i]);
>         return result;
>     }
> }
>
> alias clamp01 = Vectorize_Unary_Function!clamp01;
> ```
>
> Not exactly what you were asking for but I hope it works, 
> tricky part is I'm not sure how to generate the function name 
> without string mixins so that's why I used alias instead.

Awesome, this does exactly what I wanted. Thanks for the help! :)


More information about the Digitalmars-d-learn mailing list