【问题标题】:How to ensure the template type is scalar in D?如何确保模板类型在 D 中是标量的?
【发布时间】:2013-10-13 04:54:14
【问题描述】:

我有两个函数:一个是向量的标量乘法,另一个是向量矩阵乘法:

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

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

当然这会导致冲突,因为S 也可以是一个向量,所以第一个模板覆盖了第二个。如何告诉编译器,我只希望标量类型为 S

【问题讨论】:

    标签: templates d scalar


    【解决方案1】:

    您需要使用template constraint

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

    这声明仅当isScalarType!Strue 时才应考虑该模板。

    isScalarType 可以在std.traits 中找到。

    在 D 中,标量类型是数字类型、字符类型和bool。如果您愿意,您可以进一步限制使用来自std.traits 的其他特征(例如isNumericisFloatingPoint)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-15
      • 2012-08-14
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      相关资源
      最近更新 更多