Alternative solutions to returning ref types.
Christopher Wright
dhasenan at gmail.com
Mon Mar 17 05:44:10 PDT 2008
Simen Kjaeraas wrote:
> Just for the fun of it, I decided to try make a property struct, with
> overloaded operators. Any ideas for how to make this better are welcome.
> Of course, this causes some overhead, but was a fun project, and if it
> helps anyone, I'm a happy cat.
>
>
> Example usage:
>
> class foo
> {
> private:
> int _a;
> float _b;
> public:
> property!(int) a;
> property!(float) b;
>
> this()
> {
> a = &_a;
> b = &_b;
> }
> }
>
> -- Simen
Have you tried it with any UDTs that don't overload all those operators?
The unary operators will die; the binary ones are hidden behind
templates so they're okay unless someone uses them.
You'll need guards like:
T opNeg()
{
static if (is (typeof(-(*data))))
{
return -(*data);
}
else
{
assert (false, "type " ~ T.stringof ~ " does not support this operator."
}
}
More information about the Digitalmars-d
mailing list