Templates 如何使用;模板构造函数“;在D区?
包含一个称为“模板构造函数”的小部分。该部分没有任何示例或大量文档 我正在尝试使用这个特性(我知道我可以只使用“静态构造函数”,但我有理由更喜欢模板构造函数) 特别是,我试图在编译时生成一些散列。以下是我的尝试:Templates 如何使用;模板构造函数“;在D区?,templates,d,compile-time,dmd,Templates,D,Compile Time,Dmd,包含一个称为“模板构造函数”的小部分。该部分没有任何示例或大量文档 我正在尝试使用这个特性(我知道我可以只使用“静态构造函数”,但我有理由更喜欢模板构造函数) 特别是,我试图在编译时生成一些散列。以下是我的尝试: struct MyHash { uint value; this(uint value) { this.value = value; } this(string str)() { enum h = my
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;
}
这不是用DMD 2.053编译的:
x.d(10): Error: template x.MyHash.__ctor(string str) conflicts with constructor x.MyHash.this at x.d(5)
它抱怨第一个构造函数。移除后:
x.d(20): Error: template instance MyHash is not a template declaration, it is a struct
考虑到我使用的语法与MyHash是模板结构时的语法相同,这是非常符合逻辑的
那么,有人知道我如何声明和调用“模板构造函数”吗?我在IRC上询问过,据我们所知,它从未为D1实现过,所以我们猜测它仍然没有实现。此外,在D编程语言中没有提到这个特性,所以整个事情有点悬而未决 如果我是你,我会反对文件