Author: Miguel A. Ballicora
Date: 10:23:30 07/22/02
Go up one level in this thread
On July 22, 2002 at 04:01:23, Steffen Jakob wrote: >On July 21, 2002 at 21:36:53, Miguel A. Ballicora wrote: >> >>>1)How do I add a code with compiler switch? >>> >>you define a variable like this: >> >>#define HASH_PRUNE >> >>and then you do >> >> >>#if defined(HASH_PRUNE) >> code_1(); >>#else >> code_2(); >>#endif >> >>code_1() is compiled and NOT code_2(). Also valid options are >> >>#ifdef HASH_PRUNE >> code_1(); >>#else >> code_2(); >>#endif >> >>or >> >>#if defined(HASH_PRUNE) >> optional_code(); >>#endif > >The problem with that approach is that you actually have two different programs. >If you work for a long time with a switch enabled its likely that your code wont >compile if you disable the switch. A better solution in many cases is to use a >const boolean expression. You favourite compiler will skip the code if the >expression is false. Yes, some people prefer the approach you mention. As always, there are advantages and disadvantages. A very good thing about your approach is that the syntax is always checked. That is VERY GOOD but the problem is that some compilers complain about an "if with a constant value". I prefer to set the warnings to the maximum level and use the preprocessor approach. On the other hand, for portability purposes I like the preprocessor approach because the syntax in one branchwill be wrong and the compiler will complain about things that are not portable. Regards, Miguel > >Example: > >const bool HASH_PRUNE = true; > >if (HASH_PRUNE) { > code_1(); >} else { > code_2(); >} > >This doesnt work of course if you want to modify structs depending on such a >const. > >Greetings, >Steffen.
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.