Redefine(#define) reserved c++ key word

前端 未结 4 1056
南方客
南方客 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:31

    You can, but you shouldn't.

    In your examples, int doesn't get redefined, since it's wrapped in #ifdef int. That means "only do this if there's already a preprocessor macro called int", and there isn't.

    If you just wrote #define int 2, then all occurrences of int would be replaced by 2; but then your code wouldn't compile since 2 main() {cout<<2;} is nonsense.

    #undef will not remove a keyword from the language; it only removes preprocessor macros previously defined using #define.

提交回复
热议问题