Dlang静态模板化结构/类函数

时间:2016-05-31 22:16:02

标签: templates d

我在尝试使用下面的模板化静态函数addID时遇到了一个我无法解决的错误:

module static_example;

struct Foo(T: long) {

   public static int addID(E)(E foo1, E foo2) {
       return foo1.get() + foo2.get();
   }

   private:
       T ID;

   alias T Type;

   public this(T ID) {
       this.ID = ID;
   }

   public T get() {
       return this.ID;
   }
}

我通过电话rdmd static_example.d收到的错误是:

Error: template Foo(T : long) does not have property 'addID'

我真的不明白这个错误。我认为这不是文字标记@property的问题(似乎没有,我不会理解@property函数,所以我做了一个很少有其他结构可以在主要部分进行测试:

struct Bar {

    public static int addID(Bar bar1, Bar bar2) {
        return bar1.get() + bar2.get();
    }

    private:
        int ID;

    public this(int ID) {
        this.ID = ID;
    }

    public int get() {
        return this.ID;
    }

}

struct Batz(T: long){

    public int addID(E)(E foo) {
        return this.get() + foo.get();
    }

    private:
        T ID;

    alias T Type;

    public this(T ID) {
        this.ID = ID;
    }

    public T get() {
        return this.ID;
    }
}

void main() {
    auto foo1 = Foo!int(27);
    auto foo2 = Foo!int(13);
    int staticTemplateAdd = Foo.addID(foo1, foo2);

    auto bar1 = Bar(27);
    auto bar2 = Bar(13);
    int staticAdd = Bar.addID(bar1, bar2);

    auto batz1 = Batz!int(27);
    auto batz2 = Batz!int(13);
    int templateAdd = batz1.addID(batz2);
}

这些其他类实际上是相同的,除了Bar使用静态addIDBatz使用模板addID,而foo是静态和模板化的。

Foo是唯一会抛出错误的结构,并且我很难过。如果你感兴趣的话,我正在关注一个D模板教程(https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.md靠近顶部,但是它的例子对我来说都是外来的(和我一样),并且似乎与我试图做的有些不同。任何人都知道这里发生了什么?

0 个答案:

没有答案