您的位置:首页 > 其它

macro Vs inline

2011-01-06 08:51 489 查看
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;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: