d类型不同的模板错误

原文

module foo;

mixin template opBi(A, A function(A, A)[string] f0,){
    static foreach (k, f; f0) { A opBinary(string op: k)(A r) { return f(this, r); } }
}

struct A {
    mixin opBi!(
        A, [ "+": (A a, A b) => a],
    );
}

struct B {
    mixin opBi!(
        B, [ "+": (B a, B b) => a],
    );
}

你可以删除一些类型并使用typeof(this).来传递正确类型给f0.

module foo;

mixin template opBi(alias f0){
    static foreach (k, f; f0) { typeof(this) opBinary(string op:k)(typeof(this) r) { return f(this, r); } }
}

struct A {
    mixin opBi!(
        [ "+": (A a, A b) => a],
    );
}

struct B {
    mixin opBi!(
        [ "+": (B a, B b) => a],
    );
}

传入的AA类型是T1[string],而期望的是T2[string],其中T1:B function(B, B) pure nothrow @nogc @safe,T2:B function(B, B).

posted @ 2022-07-22 21:43  zjh6  阅读(14)  评论(0)    收藏  举报  来源