Comparison operator overloading
cym13 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Dec 6 07:01:08 PST 2015
On Sunday, 6 December 2015 at 14:41:01 UTC, Márcio Martins wrote:
> I am writing a generic numerical array struct, and I can't find
> a way to do element-wise comparison operators.
> What I had envisioned was something like the following,
> assuming a, b, c and m are array-like, and all operations
> return arrays.
> auto m = (a > b) * a + 15;
> auto c = a.choose(a > b)^^2;
> However, it seems this can't be achieved at the moment, as far
> as I can tell.
> The rewriting of a >= b to a.opCmp(b) >= 0 seems limiting in
> the sense that it assumes scalar-like operands and semantics,
> which is surprising, and goes against the awesome flexibility
> of the remaining operator overloads.
> Tried to look around, but according to google, the only
> reference I have seen to this was someone with a similar
> question in 2004, which is surprising to say the least. I
> suppose either I am missing something really obvious, or I am
> out of luck?
Don't use opCmp, all binary operators should be overriden using
opBinary. For more information I recommend this page
More information about the Digitalmars-d-learn