macro Vs inline

The C/C++ style, macros without arguments should look like variable or other identifiers, macros with arguments should look like function calls. 


so any difference between macro and inline?
#define SQUARE(x) ((x)*(x))
inline int square(int x) {return x*x;}

macro may not work properly if the actually argument is an expression with side efforts, for example:
SQUARE(i++)

The inline is newly added to C standard from C99, the previous C compiler might not support it
inline checks type of argument and does conversion, it might not work properly sometimes.for example:
double f = square(1.5);
and in C++, we can use template for avoid this issue, like 
inline const _T square_t(_T x) {return x*x;}

posted @ 2011-01-06 08:51  嗷嗷  阅读(329)  评论(0编辑  收藏  举报