如何在D中使用"模板构造器"?

dpr*_*ble 10 templates d compile-time dmd

D模板文档包含一个名为"模板构造函数"的小部分.该部分没有任何示例或大量文档.

我正在尝试使用该功能(我知道我可以使用"静态构造函数",但我有理由更喜欢模板构造函数).

特别是,我试图在编译时生成一些哈希值.这是我的尝试:

struct MyHash
{
    uint value;

    this(uint value)
    {
        this.value = value;
    }

    this(string str)()
    {
        enum h = myHashFunc(str);
        return MyHash(h);
    }
}

uint myHashFunc(string s)
{
    // Hashing implementation
    return 0;
}

int main(string[] str)
{
    MyHash x = MyHash!"helloworld";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这不能用DMD 2.053编译:

x.d(10): Error: template x.MyHash.__ctor(string str) conflicts with constructor x.MyHash.this at x.d(5)
Run Code Online (Sandbox Code Playgroud)

它抱怨第一个构造函数.删除后:

x.d(20): Error: template instance MyHash is not a template declaration, it is a struct
Run Code Online (Sandbox Code Playgroud)

考虑到我使用的语法与MyHash是模板结构相同,这是非常合乎逻辑的.

那么,有谁知道如何声明并调用"模板构造函数"?

Ber*_*ard 7

我询问了IRC,据我们所知,D1从未实现过,所以我们猜测它还没有实现.此外,没有提到D编程语言中的功能,所以整个事情有点空洞.

如果我是你,我会提交一份文件错误.