I've decided to try something. I know macros are evil and should be avoided but wanted to see what's going to happen if I do such a thing.
#include <iostream>
using namespace std;
inline void add(int x, int y) { cout << "Inline: " << x + y << endl; }
#define add(x,y) ( cout << "Macro: " << x + y << endl )
int main()
{
add(3,5);
}
It outputs:
Macro: 8
If I comment out the #define
line inline starts working, and the output turns into Inline: 8
.
My question is, why compiler decides to use macro function instead of inline. Thank you!
I'm using
Linux Mint 18.2
,g++ 5.4.0
, with no parametersg++ -g t2.cpp -o t2
.
Macro substitution is performed via the pre-processor before compilation. Thus the compiler never sees add(3,5)
- it only sees the macro expansion.