Is it possible to use #define inside a function?

前端 未结 6 1894
野性不改
野性不改 2021-01-07 22:38

For example, I saw source code like the following. Can we use #define in a function? How does it work? (more information: this code is what I copied from

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 23:05

    Sure. #define is handled by the preprocessor which occurs well before the compiler has any sense of lines of code being inside functions, inside parameters lists, inside data structures, etc.

    Since the preprocessor has no concept of C++ functions, it also means that there is no natural scope to macro definitions. So if you want to reuse a macro name, you have to #undef NAME to avoid warnings.

提交回复
热议问题