Request assistance converting C's #ifndef to D

Joseph Rushton Wakeling via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 16 09:15:31 PDT 2016


On Thursday, 12 May 2016 at 22:51:17 UTC, Andrew Edwards wrote:
> The following preprocessor directives are frequently 
> encountered in C code, providing a default constant value where 
> the user of the code has not specified one:
>
> 	#ifndef MIN
> 	#define MIN     99
> 	#endif
>
> 	#ifndef MAX
> 	#define MAX     999
> 	#endif

One option here is that the programmer is trying to avoid 
multiple definitions of MIN and MAX if for some reason this 
header is included together with another header that also defines 
a MIN and MAX.

So, you might start by checking if any other header/source file 
does so.  It's entirely possible the programmer is just going 
overkill with the kind of stuff one does to guard against 
multiple #include's of the same header, and that this header is 
the _only_ place where MIN and MAX are defined, and that it's 
ALWAYS valid for them to be 99 and 999 respectively.

The other thought is that the programmer might have in mind to be 
able to choose alternative MIN and MAX at compile time via 
environment variables (perhaps the project's build scripts make 
use of this?).  If you think so, is that something you want to 
support?  There are probably better ways of achieving the same 
result.

I suspect it'll probably turn out to be fine to just use

     enum MIN = 99;
     enum MAX = 999;

... but H. S. Teoh's suggestion looks sane as a more cautious 
alternative.


More information about the Digitalmars-d-learn mailing list