How to ensure the template type is scalar in D? -
i have 2 functions: 1 scalar multiplication vector , other - vector-matrix multiplication:
pure t[] mul(s, t)(s s, t[] a) and
pure t[] mul(t)(t[] a, t[][] b) of course leads conflict, s can vector too, first template covers second. how tell compiler, want scalar type s?
you need use template constraint.
pure t[] mul(s, t)(s s, t[] a) if (isscalartype!s) this declares template should considered when isscalartype!s true.
isscalartype can found in std.traits.
in d, scalar types numeric types, character types, , bool. can restrict further using other traits std.traits if (e.g. isnumeric or isfloatingpoint).
Comments
Post a Comment