Request assistance converting C's #ifndef to D

Andrew Edwards via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 12 23:05:14 PDT 2016


On 5/13/16 7:51 AM, 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
>
> I'm at a loss at how to properly convert it to D. I've tried the following:
>
>      enum MIN = 0;
>      static if(MIN <= 0)
>      {
>          MIN = 99;
>      }
>
> it works as long as the static if is enclosed in a static this(),
> otherwise the compiler complains:
>
>      mo.d(493): Error: no identifier for declarator MIN
>      mo.d(493): Error: declaration expected, not '='
>
> This however, does not feel like the right way to do thinks but I cannot
> find any documentation that provides an alternative. Is there a better
> way to do this?
>
> Thanks,
> Andrew

Additionally, what's the best way to handle nested #ifdef's? Those that 
appear inside structs, functions and the like... I know that global 
#ifdef's are turned to version blocks but versions blocks cannot be used 
inside classes, stucts, functions, etc.


More information about the Digitalmars-d-learn mailing list