문제

I have two functions: one is scalar multiplication for a vector and other - vector-matrix multiplication:

pure T[] mul(S, T)(S s, T[] a)

and

pure T[] mul(T)(T[] a, T[][] B)

Of course this leads to a conflict, as S can be a vector too, so that first template covers the second. How do I tell compiler, I want only scalar type to be S?

도움이 되었습니까?

해결책

You need to use a template constraint.

pure T[] mul(S, T)(S s, T[] a) if (isScalarType!S)

This declares that the template should only be considered when isScalarType!S is true.

isScalarType can be found in std.traits.

In D, the scalar types are the numeric types, character types, and bool. You can restrict further using the other traits from std.traits if you like (e.g. isNumeric or isFloatingPoint).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top