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