0
点赞
收藏
分享

微信扫一扫

d类型不同的模板错误

七千22 2022-07-27 阅读 83


​​原文​​

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)​​.


举报

相关推荐

0 条评论