سؤال

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