preprocessor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    preprocessor

    I have a preprocessor constant (SUPPORT_PNG_IM AGE) with 3 states, to control the compilation:
    ---------
    #undef SUPPORT_PNG_IMA GE
    #define SUPPORT_PNG_IMA GE
    #define SUPPORT_PNG_IMA GE ONLY
    ---------
    With the 1st & 3rd line the following line works
    ---------
    #if SUPPORT_PNG_IMA GE == ONLY
    ---------
    but with the 2nd, compiler throws:
    ---------
    fatal error C1017: invalid integer constant expression
    ---------

    how can I handle this?

    thanks


  • Leor Zolman

    #2
    Re: preprocessor

    On Sat, 28 Feb 2004 03:55:05 +0200, "<- Chameleon ->"
    <cham_gss@hotma il.NOSPAM.com> wrote:
    [color=blue]
    >I have a preprocessor constant (SUPPORT_PNG_IM AGE) with 3 states, to control the compilation:
    >---------
    >#undef SUPPORT_PNG_IMA GE
    >#define SUPPORT_PNG_IMA GE
    >#define SUPPORT_PNG_IMA GE ONLY
    >---------
    >With the 1st & 3rd line the following line works
    >---------
    >#if SUPPORT_PNG_IMA GE == ONLY
    >---------
    >but with the 2nd, compiler throws:
    >---------
    >fatal error C1017: invalid integer constant expression
    >---------
    >
    >how can I handle this?[/color]

    I'd suggest sticking with one of the following conventions or the other for
    defining SUPPORT_PNG_IMA GE:

    Convention #1: Either it is defined or it isn't.

    Convention #2: It has a non-empty value, and we'll be testing it.

    The first convention, testable with any of the following:

    #ifdef SUPPORT_PNG_IMA GE
    #ifndef SUPPORT_PNG_IMA GE
    #if defined(SUPPORT _PNG_IMAGE)
    #if !defined(SUPPOR T_PNG_IMAGE)

    is good enough for either/or situations. The second, testable with

    #if SYMBOL == whatever

    is suitable for having many mutually exclusive conditional compilation
    blocks.

    You /can/ get where you want to go with the convention you've already
    established, if you absolutely have to:

    #if !defined(SUPPOR T_PNG_IMAGE) // your 1st case

    #if defined(SUPPORT _PNG_IMAGE) && \
    SUPPORT_PNG_IMA GE != ONLY // case 2

    #if defined(SUPPORT _PNG_IMAGE) && \
    SUPPORT_PNG_IMA GE == ONLY // case 3

    but it is not very orthogonal. BTW, ONLY has been previously #defined,
    right?
    -leor



    [color=blue]
    >
    >thanks
    >[/color]

    Leor Zolman
    BD Software
    leor@bdsoft.com
    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
    Decryptor at www.bdsoft.com/tools/stlfilt.html

    Comment

    • Andrey Tarasevich

      #3
      Re: preprocessor

      <- Chameleon -> wrote:
      [color=blue]
      > I have a preprocessor constant (SUPPORT_PNG_IM AGE) with 3 states, to control the compilation:
      > ---------
      > #undef SUPPORT_PNG_IMA GE
      > #define SUPPORT_PNG_IMA GE
      > #define SUPPORT_PNG_IMA GE ONLY
      > ---------
      > With the 1st & 3rd line the following line works
      > ---------
      > #if SUPPORT_PNG_IMA GE == ONLY
      > ---------
      > but with the 2nd, compiler throws:
      > ---------
      > fatal error C1017: invalid integer constant expression
      > ---------
      >
      > how can I handle this?
      > ...[/color]

      I assume that 'ONLY' is not #defined anywhere. Right?

      In the first case (when 'SUPPORT_PNG_IM AGE' is not defined) the
      preprocessor replaces both 'SUPPORT_PNG_IM AGE' and 'ONLY' with '0',
      which means that your '#if' will turn into

      #if 0 == 0

      In the second case (when 'SUPPORT_PNG_IM AGE' is defined as nothing),
      your '#if' will turn into

      #if == 0

      In the third case after full replacement you'll again end up with

      #if 0 == 0

      Note, that the second variant is invalid (that's what causes the error).
      Not also that the first and the third variant produce the same result.

      Maybe you should try to do something like this instead

      #define SPI_NONE 0
      #define SPI_ONLY 1
      #define SPI_ALL 2

      then do

      #define SUPPORT_PNG_IMA GE SPI_ONLY // or SPI_NONE, or SPI_ALL

      and implement the '#if' as follows

      #if SUPPORT_PNG_IMA GE == SPI_ONLY
      ...

      or

      #if SUPPORT_PNG_IMA GE < SPI_ALL
      ...

      etc.

      --
      Best regards,
      Andrey Tarasevich

      Comment

      Working...