inline还是macro

简介:

inline还是macro?

这个问题实际上很简单,inline是函数,当你需要写一个函数的时候就用inline。
而macro呢?它有超越函数的能力,它可以传递无类型的参数、可以拼装模板化的代码、甚至可以拼装token。这些东西都是函数,甚至C++模板都无法办到的。

顺便贴一段用macro写的打印log的代码吧 :)

#define _ALERT 0
#define _CRIT 1
#define _ERR 2
#define _WARNING 3
#define _NOTICE 4
#define _INFO 5
#define _DEBUG 6

#define DP_ALERT_TEXT "ALERT:"
#define DP_CRIT_TEXT "CRITICAL:"
#define DP_ERR_TEXT "ERROR:"
#define DP_WARNING_TEXT "WARNING:"
#define DP_NOTICE_TEXT "NOTICE:"
#define DP_INFO_TEXT "INFO:"
#define DP_DEBUG_TEXT "DEBUG:"

static int debug_level;

#define DEBUG(lev, fmt, args...) \
do { \
if (debug_level > (lev)){ \
fprintf(stderr, "%d [%d] " DP##lev##_TEXT fmt, time(), getpid(), __FUNCTION__, ## args); \
} \
}while(0)

#define LM_ALERT(fmt, args...) DEBUG(_ALERT, fmt, ##args)
#define LM_CRIT(fmt, args...) DEBUG(_CRIT, fmt, ##args)
#define LM_ERR(fmt, args...) DEBUG(_ERR, fmt, ##args)
#define LM_WARN(fmt, args...) DEBUG(_WARNING, fmt, ##args)
#define LM_NOTICE(fmt, args...) DEBUG(_NOTICE, fmt, ##args)
#define LM_INFO(fmt, args...) DEBUG(_INFO, fmt, ##args)
#define LM_DBG(fmt, args...) DEBUG(_DEBUG, fmt, ##args)


目录
相关文章
|
5月前
|
安全 编译器 程序员
15 C++ - 内联函数(inline function)
15 C++ - 内联函数(inline function)
54 0
|
2月前
|
Linux
offsetof宏与container_of宏
offsetof宏与container_of宏
12 0
block、inline、inline-block的区别
block、inline、inline-block的区别
|
11月前
|
前端开发 容器
CSS: inline、block和inline-block的区别
CSS: inline、block和inline-block的区别
|
编译器 C++
Effective C++条款 02:尽量以 const, enum, inline 替换 #define
Effective C++条款 02:尽量以 const, enum, inline 替换 #define
109 0
|
编译器 C++
尽量以const、enum、inline替换#define
尽量以const、enum、inline替换#define
154 0
尽量以const、enum、inline替换#define
宏"__FUNCTION__" 和" __LINE__ "
宏"__FUNCTION__" 和" __LINE__ "
224 0