【发布时间】:2013-01-25 22:32:18
【问题描述】:
struct Type
{
auto opBinary(string op)(Type other) const {
return Type(); // #1 return type is Type
return typeof(this)(); // #2 return type is const(Type)
}
}
unittest
{
Type t1, t2;
auto t3 = t1 + t2;
}
在t1.opBinary!("+")(t2) 中,t1 变为常量,而t2 保持非常量。 opBinary的返回类型应该是Type还是const(Type),为什么?
const(T) 是一个超类型,所以也许它应该返回一个const,但我在实践中几乎没见过这种情况。在处理使用这些类型或被这些类型使用的类型和函数的层次结构时,事情也会变得相当复杂。
【问题讨论】: