import std.stdio; import std.traits; import std.exception; struct CheckedInt(N) if (isIntegral!N) { private N value; ref CheckedInt opUnary(string op)() if (op == "++") { enforce(value != value.max); ++value; return this; } this(N _value) { value = _value; } }
I didn't know you could define a return type of a templated struct without defining the type it is parameterized on. I mean this line: ref CheckedInt opUnary(string op)() if (op == "++") I thought for sure I always had to write the parameterized type like so: ref CheckedInt!(N) opUnary(string op)() if (op == "++") So I guess this really isn't a question but more of a "oh, I didn't know you could do that". In fact I rarely see this kind of code in Phobos, most of the time the parameterized type is specified in these types of cases. Is this feature described somewhere, because I must have missed it if it is? As a side-note, auto ref is useful in this case, which is pretty cool: auto ref opUnary(string op)() if (op == "++")