Use of templates to avoid redudancy of code
Fawzi Mohamed
fmohamed at mac.com
Tue Jun 3 06:19:33 PDT 2008
On 2008-06-03 11:39:24 +0200, Mael <mael.primet at gmail.com> said:
> Hello,
>
> I'm writing an algorithm that has, say, too possible behaviour
> depending on a switch. Since there are many imbricated loops, and the
> behaviour change is inside the loop, testing for the flag is
> time-consuming
I trust you really profiled the code to see this (never optimize before
testing)
> for( ....)
> for( .... )
> for( ... )
> {
> if( flag ) action_1 ;
> else action_2 ;
> }
>
> is there a clean way to use templates to generate the duplicate of the
> code like
>
> if( flag )
> {
> for(...) .... action1 ;
> }
> else
> {
> for(...) .... action2 ;
> }
a simple compile time solution is to make the looping construct a
template, and the action a template parameter (either alias or char[]
to mixin if you need).
I used it extensively in my multidimensional array lib...
More information about the Digitalmars-d-learn
mailing list