Operator Overloading with class template

Francois Chabot francois.chabot.dev at gmail.com
Sun Apr 8 16:41:50 PDT 2012


On Sunday, 8 April 2012 at 23:14:33 UTC, Eyyub wrote:
> Hello,
>
> How can I rewrite the exemple 2 (http://pastebin.com/q50903Zh) 
> in D lang. ?
> This source code doesn't work...why ?
> http://paste.pocoo.org/show/wy1kDIpqTi2ApRuOxRRb/
>
> Thx. :)

As far as I know, there is nothing special about themplate 
classes with regards to operator overloading, so you can follow 
the examples in there: http://dlang.org/operatoroverloading.html

which in your example would yield:

Matrix opOpAssign(string op)( Matrix rhs )
{	
   for( size_t i =0 ; i < M ; ++i )
   {
     for( size_t j =0 ; j < N ; ++j )
     {
       mixin("array_[i*N+j]" ~ op ~ " rhs.array_[i*N+j] ;") ;
     }
   }
}

Bonus! all ...= operators implemented at once!


More information about the Digitalmars-d-learn mailing list