Author: Sven Reichard
Date: 01:42:33 11/26/03
Go up one level in this thread
On November 24, 2003 at 09:46:59, Reinhard Scharnagl wrote: >obviously there finally have to be two procedures, so source code >has to be interpreted twice. Using *.C or *.CPP files leads to >semantic problems, so templates will be an appropriate idea for >a good solution - having code placed in *.H or *.HPP files. > >I want to avoid any non-header source code to be compiled twice. >May be this is a strange problem of a somehow puristic nature. > >Regards, Reinhard. If you use templates, it will be compiled twice (at least with the compilers I work with). If you use a function and pass the color as a parameter, it will be compiled once. Within the function, you can avoid duplication by extracting the code that really depends on the color. E.g., I would turn the following fictional code void pawnCapture(Square source, Color c) { if (c == WHITE) { if (color(board[source+9]) == BLACK) generateCapture(source, source+9); if (color(board[source+7) == BLACK) generateCapture(source, source+7); } else { if (color(board[source-9]) == WHITE) generateCapture(source, source-9) if (color(board[source-7]) == WHITE) generateCapture(source, source-7); } }, which contains lots of duplication, into something like Color otherColor(Color c) { return (c==WHITE)?BLACK:WHITE; }; int forward(Color c) { return (c==WHITE)? 8: -8; }; void pawnCapture(Square source, Color c) { int fwd = forward(c); for (int disp = -1; disp <= 1; disp += 2) { Square dest = source + fwd + disp; if (color(board[dest]) == otherColor(c)) generateCapture(source, dest); } }; which avoids the duplication. Moreover, you can reuse the functions depending on the color in other parts of the code. This is just my personal view of things. Some may argue that the additional function calls are slow; then you can always declare them as inline, which leads to duplication of code. Cheers, Sven.
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.