Custom binary operators

John Colvin john.loughran.colvin at gmail.com
Sat Dec 28 09:50:49 PST 2013


On Saturday, 28 December 2013 at 16:33:24 UTC, Joseph Rushton 
Wakeling wrote:
> On 28/12/13 16:24, Dicebot wrote:
>> AFAIK it is intentionally banned to constrain operator 
>> overloading abuse.
>
> Ahh, makes sense.  But isn't it possible to do something with 
> templates that would allow for something like,
>
>     auto a = matrix(...);
>     auto b = matrix(...);
>
>     auto c = ElementWise!("a * b");
>
> ... where the ElementWise template would ensure that the binary 
> operation is applied successively to corresponding pairs of 
> elements from the matrices?

how about:

a.elementWise * b

where a.elementWise returns a wrapper struct around a that 
implements elementwise arithmetic as opposed to the normal 
arithmetic.

The operators would return instances of Matrix, not elementWise, 
to avoid accidentally spilling a Matrix.ElementWise struct in to 
ensuing code unintentionally. With a shorter alias it's rather 
neat.

auto e = ((a .EW* b) .EW/ c ) * d;


More information about the Digitalmars-d-learn mailing list