class 模板中的纯构造函数

Pure constructors in class templates

我有一些有效的代码,但我不确定为什么。我将 class 实例化为不可变变量。 class 中没有 immutable 构造函数,none 被标记为 pure,但它仍然可以正常工作。

我还读到 pure 构造函数可以全面用于可变、不可变、常量和共享实例

我在D网站上唯一能找到的就是在函数模板中推断纯度。我是否应该假设因为我的 class 是参数化的(或 class 模板)编译器正在推断所有方法的纯度,包括构造函数?

代码如下:

public class Data(size_t numInputs, size_t numTargets)
{
  ...
  public this(in double[][] data, in bool[] filter, in bool doNorm = true)
  {
    ...
  }
}

The only thing I can find on the D website is that purity is inferred in function templates. Should I assume that since my class is parameterized (or a class template) that the compiler is inferring the purity of all the methods, including the constructor?

是的。由于 class 是一个模板,所有的方法也是模板(考虑到它们必须是,因为隐藏的 this 参数的类型来自模板)。因此,它们的 body 必须在源代码中可用。推断属性的两个要求是 body 源可用和它们的模板参数(而且函数不能尝试调用或检查自身,但你也不要这样做)因此它可以被推断为纯.