2

The following code does NOT generate the #error, but instead compiles, runs, and output the trace value 37 (TT_LAST_PARM equals 53 and TT_FIRST_PARM equals 16):

#if ((TT_LAST_PARM - TT_FIRST_PARM) >= 32)
#error More than 32 parm tokens
#else
HTRACE("%d", TT_LAST_PARM - TT_FIRST_PARM);
#endif

If I hard code the values,

#if ((53 - 16) >= 32)
#error More than 32 parm tokens
#else
    HTRACE("%d", 53 - 16);
#endif

the pre-processor DOES generate the error:

Error 1 fatal error C1189: #error : More than 32 parm tokens d:\codeMTX\Knowbase\KBMatL\PrintParser.CPP 2663

franji1
  • 3,088
  • 2
  • 23
  • 43
  • 1
    You can't do this with the pre-processor - you need a compile-time assert. – Paul R Jun 03 '16 at 16:17
  • 2
    The pre-processor does not know about enum names; to the preprocessor they are just undefined symbols – antlersoft Jun 03 '16 at 16:19
  • @antlersoft that makes sense - but I really don't like using #defines. Why doesn't it flag as a PREPROCESSOR error, e.g. #if XYZZY? I thought I would have to do #if defined(XYZZY) && XYZZY??? – franji1 Jun 03 '16 at 16:22
  • @PaulR I had the assert originally, but I try to flag errors at compile time when they can (should). – franji1 Jun 03 '16 at 16:24
  • @franji1: yes, that's why I said you need a [compile-time assert](http://stackoverflow.com/q/6765770/253056) (i.e. not a run-time assert). – Paul R Jun 03 '16 at 16:25
  • @PaulR awesome! But I am using VS2008. Stuck with the runtime assert. But if/when I get there, I will DEFINITELY look into that!! – franji1 Jun 03 '16 at 16:31
  • @franji1 There are ways to get a compile-time assertion in C++03. Boost.StaticAssert is one. – Angew is no longer proud of SO Jun 03 '16 at 16:39

1 Answers1

7

enum is a C/C++ keyword, not a pre-processor directive.
Pre-processor directives are clearly denoted by a leading # symbol.

The pre-processor doesn't have/know a symbol called TT_LAST_PARM etc, and according to the C++ standard (§16.1 ¶4):

After all replacements due to macro expansion and the defined unary operator have been performed, all remaining identifiers and keywords, except for true and false, are replaced with the pp-number 0

So your expression evaluates to (0 - 0) >= 32

kfsone
  • 23,617
  • 2
  • 42
  • 74