I do not understand the behaviour of #define macro in C++

后端 未结 4 1365
既然无缘
既然无缘 2021-01-29 14:09

I need to understand how this code works:

#define foo1( a ) (a * a)              // How does this work?
inline int foo2(         


        
4条回答
  •  轮回少年
    2021-01-29 14:58

    Macros are not functions.

    Macros do TEXT replacement. So when you have

    #define foo1( a )   (a * a)
    

    any instance of foo1( ... ) with anything between then parenthesis will be expanded AS TEXT, not as an expression. So when you have foo1( 1 + 2 ) it turns into ( 1 + 2 * 1 + 2 )

提交回复
热议问题