Templated struct doesn't need the parameterized type in return type definitions?

Andrej Mitrovic none at none.none
Tue Mar 8 09:06:08 PST 2011


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 == "++")


More information about the Digitalmars-d-learn mailing list