Is it possible to use #define inside a function?

前端 未结 6 1930
野性不改
野性不改 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 this is possible. The #define is processed by the preprocessor before the compiler does anything. It is a simple text replacement. The preprocessor doesn't even know if the line of code is inside or outside a function, class or whatever.

    By the way, it is generally considered bad style to define preprocessor macros in C++. Most of the things they are used for can be better achieved with templates.

提交回复
热议问题