d生成重载函数

float clamp01(float x) { return x < 0 ? 0 : (x > 1 ? 1 : x); }

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;


void main() {
    float[5] vec = [1,2,3,4,5];
    float[5] other = clamp01(vec);
    writeln(vec);
    writeln(other);
}
posted @ 2021-05-05 08:38  zjh6  阅读(13)  评论(0)    收藏  举报  来源