Topic: Proposal for new preprocessor directives


Author: "AllanW" <AllanW@my-deja.com>
Date: 1999/12/07
Raw View
I proposed this in another thread, but it was ignored.

The idea is to allow third-party vendors to avoid collisions with
user-defined macros. We would add two new directives to the preprocessor:

    #macro_off
would disable expansion of macros, except in preprocessor-expressions, and
    #macro_on
would re-enable them. These directives could be nested, so that
    #macro_off
        // Macros are now off
        #macro_off
        #macro_on
    // Macros are still off
    #macro_on
    // Now macros are re-enabled

Note that disabling macros is not the same as disabling the preprocessor;
macros could still be checked with i.e. #ifdef or #if defined(). However,
code other than preprocessor would not be affected by macros.

Example:

    // Please don't ever really redefine int!
    #define int double
    int a;
    #macro_off
      int b;
      #macro_off
        #ifdef int
          // Note that macro "int" is still defined,
          // even though it doesn't expand in code
          #undef int
          #define int char
          int c;
        #else
          #error // Shouldn't compile this line!
        #endif
      #macro_on
      int d;
    #macro_on
    int e;
    #undef int

is equivalent to:

    double a;  // because "int" was redefined as "double"
    int b;     // because macros are disabled
    int c;     // because macros are disabled
    int d;     // because macros are disabled
    char e;    // Because "int" was redefined as "char"

Typical usage:

    // ThirdParty.H
    #macro_off
    // Note that with macros OFF, #ifndef and #define still work
    #ifndef THIRD_PARTY_INCLUDED
    #define THIRD_PARTY_INCLUDED
    namespace ThirdParty {
        class Class1 {
            int data;
            // ...
        };
        class Class2 {
            int data;
            // ...
        };
        class Class3 {
            int data;
            // ...
        };
    }; // Namespace ThirdParty
    #endif // THIRD_PARTY_INCLUDED
    #macro_on // Re-enable

This will have the intended effect, even if the user defined macros such as
data, Class2, int, etc.



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]