unittest
{
immutable float a = 1.1, b = 1.2;
auto c1 = complex(a,b);
auto r1 = c1 + c1; // error, not mutable
}
这意味着我可以拥有Complex!(immutable float)
,但我永远不能将它的opBinary
功能用于Complex!(immutable float)
. 那么,为什么 std.complex 会以这种方式实现呢?
这里是opBinary
. 它调用opOpAssign
,它不适用于immutable
,因此出现错误。
Complex!(CommonType!(T,R)) opBinary(string op, R)(Complex!R z) const
{
alias typeof(return) C;
auto w = C(this.re, this.im);
return w.opOpAssign!(op)(z);
}