d语言之模块重载
参考自d程序设计语言---我的博客http://my.oschina.net/u/218155/blog?fromerr=SwOkb7Sw fllow me
import std.conv;
import std.stdio;
//import std.Exception;
struct CheckedInt(N)if(isIntegral!N){
private N value;
this(N value){
this.value = value;
}
ref CheckedInt opUnary(string op)()if(op == "++"){
assert(value != value.max);
++value;
return this;
}
ref CheckedInt opUnary(string op)()if(op == "--"){
assert(value != value.min);
--value;
return this;
}
CheckedInt opUnary(string op)()
if(op =="+" || op == "-" || op == "~")
{
return CheckedInt(mixin(op~"value"));
}
//opt cast
//string opCast(T)()if(is(T == string)){}
//opt +-*/
//CheckedInt opBinary(string op)(CheckedInt){}
@property void showValue(){
writeln(value);
}
}
void main() {
CheckedInt!int ct = CheckedInt!int(11);
ct++;
--ct;
-ct;
ct.showValue;
writeln("t");
}