Redefine(#define) reserved c++ key word

前端 未结 4 1050
南方客
南方客 2021-01-26 12:20

Is it possible to redefine a c++ keyword using #define?

#ifdef int
#undef int 
#define int 2
#endif
int main(){
    //Do something with int
}

I

4条回答
  •  粉色の甜心
    2021-01-26 12:46

    Is it possible? Yes. Is it good style? Absolutely not.

    The preprocessor is not aware of C/C++ keywords, it only knows about preprocessor tokens and just does strict text replacement.

    Your example is resulting in an error because you're #undefing it. Once you undefine it, it reverts to its previous behavior.

    The only valid use I know of for doing something like this is to work around a bug in an old compiler, and that compiler is no longer relevant these days.

提交回复
热议问题