Skip to content

Wrong operation of the preprocessor with the enum type. #10138

@allnick

Description

@allnick

Hi.
Here are two seemingly equivalent versions of program code.

=============================================================

#define BOARD_MODE_NONE 0
#define BOARD_MODE_1    1
#define BOARD_MODE_2    2
#define BOARD_MODE_1_2  (BOARD_MODE_1 + BOARD_MODE_2)
#define BOARD_MODE_HARD 255

#define BOARD_MODE      BOARD_MODE_1_2

#if BOARD_MODE == BOARD_MODE_HARD // It's false. Block 2 is defined. It's OK.
  // Some block 1
#else
  // Some block 2
#endif

=============================================================

enum tBoardModes{
  BOARD_MODE_NONE = 0,
  BOARD_MODE_1    = 1,
  BOARD_MODE_2    = 2,
  BOARD_MODE_1_2  = (BOARD_MODE_1 + BOARD_MODE_2),
  BOARD_MODE_HARD = 255
};

#define BOARD_MODE  BOARD_MODE_1_2

#if BOARD_MODE == BOARD_MODE_HARD // It's true! Block 1 is defined! What a hell !!!
                                  // However, the values of all IDs remain correct !!!
  // Some block 1
#else
  // Some block 2
#endif

=============================================================

As it turned out, they are not so equivalent…
Bye.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: InvalidOff topic for this repository, or a bug report determined to not actually represent a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions