Re: String comparison in preprocessor commands





Erik Leunissen wrote:
> L.S.
>
> How can I make certain code parts be compiled conditionally, depending
> on the definition of a macro such as:
>
> #define VERSION "2.3"
>
> Is it all right to do things like:
>
>
> #if VERSION == "2.3"
> ..../* conditionally compiled code */
> #endif
>
> #if VERSION != "2.3"
> ..../* conditionally compiled code */
> #endif
>
> #if VERSION > "2.3"
> ..../* conditionally compiled code */
> #endif

No, that won't work: The preprocessor can generate
string literals, but it can't actually work with strings.
(In particular, it can't compare them.)

What you *can* do, which may be satisfactory for some
purposes, is use the preprocessor to generate a compile-
time constant, test that value with `if' instead of `#if',
and rely on the compiler to eliminate dead code:

#define MAJOR (VERSION[0] - '0')
#define MINOR (VERSION[2] - '0')
if (MAJOR == 2 && MINOR == 3) { ... }
if (MAJOR != 2 || MINOR != 3) { ... }
if (MAJOR > 2 || (MAJOR == 2 && MINOR > 3)) { ... }

However, this trick will only work for executable statements;
you can't use it to "conditionally compile" declarations and
the like. Also, it will break pretty badly if VERSION ever
becomes "2.10" or "10.0" ...

> Or should I go about this differently? Please note that I'm not in the
> position to change the macro definition.

If you really cannot change the macro definition, things
are going to be messy. The best I can suggest is to use a
"helper" program as suggested by Anonymous 7843. (However,
note that his suggestion of converting VERSION to `double'
is not very robust; consider the "2.10" case. The '.' in
a version number is a field separator, not a decimal point.)

If you must leave VERSION's string-ness and value intact
but are allowed to change the way it's defined, things can
be lots easier. Instead of trying to extract MAJOR and MINOR
from VERSION, you could use MAJOR and MINOR as the "primary
sources" and derive VERSION from them:

#define MAJOR 2
#define MINOR 3

#define STRING(x) STR_HELPER(x)
#define STR_HELPER(x) #x
#define VERSION STRING(MAJOR) "." STRING(MINOR)

This gives VERSION the same value as before, but makes MAJOR
and MINOR available for straightforward preprocessor tests.

--
Eric.Sosman@xxxxxxx

.



Relevant Pages

  • Re: How to define a const and initialize it?
    ... > do the same work as const int MYCONST will do.... ... But the thing is that macro substitution takes ... C++ compiler isn't even aware of its existence. ... This Item might better be called "prefer the compiler to the preprocessor," because #define is often ...
    (alt.comp.lang.learn.c-cpp)
  • Re: #define and (brackets)
    ... The compiler doesn't second-guess. ... STRIZE is passed a value x as NUM. ... The preprocessor again creates ... it must evaluate the macro but this time instead of #NUM ...
    (microsoft.public.vc.language)
  • Re: #ifdef inside macro expansion
    ... The preprocessor is what you are looking at - not the compiler. ... a macro with, so this is expected bhavior - not a bug. ...
    (microsoft.public.vc.language)
  • Re: gcc 4.3 warnings
    ... A few people at work have mentioned it seems unusual for a preprocessor ... A macro definition has to consist of a sequence of tokens. ... A string ... preprocessor documentation. ...
    (Fedora)
  • Re: Preprocessing directive in the middle of macro arguments
    ... this compiler seems confused by any preprocessing ... conditionaly select one of several arguments passed to a macro. ... All preprocessor directives are processed before any macro expansion ...
    (comp.lang.c)