Redefine(#define) reserved c++ key word

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

    If you're don't use the standard libraries you're allowed to do so. In fact the preprocessor shouldn't distinguish between reserved and non-reserved words.

    However that's probably not why you run into problems. First of all your examples don't do what you probably think. The fault is that int is normally not a preprocessor defined macro. The #ifdef int directive will therefore skip the following lines up to the terminating #endif.

    What this means is that your second example expands to:

    // stuff from iostream and possibly other headers 
    
    int main(){
        cout<

    the fault is that cout< simply isn't allowed.

提交回复
热议问题