Request assistance converting C's #ifndef to D
sanjayss via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri May 13 10:34:20 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
>
> 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
One thing you could try is compile the C code without the
#ifndef's and see if it compiles without issues. If it does,
simply use a D enum and don't worry about translating the
#ifndef's.
(Alternately check the C code to see if there really are
differing definitions of MIN/MAX -- if there are, then the code
is already messed up and you need to implement your D code taking
into consideration the implications of that -- maybe use
different variable names in each section of the code for the
MIN/MAX that takes the different MIN/MAX values. I've typically
seen this kind of ifdef'ing to quick-fix compile issues in new
code without having to rewrite a whole bunch of existing code or
code-structure and it is never to have differing values for the
defines -- if differing values are used with the same name, then
the code is bad and it's best to cleanup as you migrate to D.)
More information about the Digitalmars-d-learn
mailing list