当前位置:网站首页>变长参数__VA_ARGS__ 和 写日志的宏定义
变长参数__VA_ARGS__ 和 写日志的宏定义
2022-04-23 13:59:00 【面试被拒1万次】
变长参数__VA_ARGS__ 和 写日志的宏定义
宏定义带’#'字符的不同含义
带一个 ‘#’ 转化为字符串
#define _TOSTRING(x) #x
int main()
{
auto s1 = _TOSTRING(123);
std::cout << " s1 " <<s1 <<std::endl; // "123" 返回const char*
}
带两个 “##” 就是拼接(主要是跟 ##__VA_ARGS__一起用)
用于宏的替换部分,这个运算符把两个语言符号组合成单个语言符号,为宏扩展提供了一种连接实际变元的手段。也可以用作数字拼接吧,
不过要注意int的取值范围,我用这个宏去替换(连接两个字符串)会报错,是错误的参数,至于为啥返回int,我还没搞明白,如果有人知道告
诉我一下
#define CONNECT_TWO(x,y) x##y
int main()
{
auto s2 = CONNT_TWO(123,456);
std::cout << " s2 " <<s2 <<std::endl; // 返回的是一个int的数字拼接,123456
}
定义日志宏
第一种:日志宏,注意一点:在define中如果是多行定义,需要加上符号 '\'
#define LOG(...){ \
fprintf(stderr,"File:%s Line:%d Func:%s \t",__FILE__,__LINE__,__func__ );\
fprintf(stderr,__VA_ARGS__);\
fprintf(stderr,"\n");\
}
第二种:我们先理解一下 预定义宏 VA_ARGS
// 预定义的宏 __VA_ARGS__ 可以在宏定义的实现部分替换省略号所代表的字符串
// 在宏定义中 使用__VA_ARGS__ 替换 ...
#define myPrintf(...) printf(__VA_ARGS__)
在使用c++ 编译的时候需要注意,在变量和字符串连接的时候需要加上空格,所以下面在fmt 左右都留有空格
#define error_printf(fmt,...) printf("[ERROR][%s]( %s | %d )" fmt "\n",__FILE__,__FUNCTION__,__LINE__,##__VA_ARGS__)
以上 宏定义 都可以当变量使用
__FILE__ : 返回的是文件名
__LINE__ : 返回的是行数
__FUNCTION__ : 返回的是函数名
上面是自己整合的一些理解,参考博客地址:
添加链接描述
版权声明
本文为[面试被拒1万次]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_38023160/article/details/115708459
边栏推荐
- [code analysis (2)] communication efficient learning of deep networks from decentralized data
- Problems encountered in the project (V) understanding of operating excel interface poi
- About me
- Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]
- Building MySQL environment under Ubuntu & getting to know SQL
- SQL learning | complex query
- Leetcode brush question 897 incremental sequential search tree
- 【项目】小帽外卖(八)
- [code analysis (1)] communication efficient learning of deep networks from decentralized data
- Oracle告警日志alert.log和跟踪trace文件中文乱码显示
猜你喜欢
Dynamic subset division problem
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
大专的我,闭关苦学 56 天,含泪拿下阿里 offer,五轮面试,六个小时灵魂拷问
Port occupied 1
ACFs file system creation, expansion, reduction and other configuration steps
Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
【vmware】vmware tools 地址
Neuron and neural network
记录一个奇怪的bug:缓存组件跳转之后出现组件复制
Express②(路由)
随机推荐
Handling of high usage of Oracle undo
Apache seatunnel 2.1.0 deployment and stepping on the pit
JS force deduction brush question 103 Zigzag sequence traversal of binary tree
About note 1
scikit-learn构建模型的万能模板
New关键字的学习和总结
JS brain burning interview question reward
Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"
JS 力扣刷题 102. 二叉树的层序遍历
About me
大专的我,闭关苦学 56 天,含泪拿下阿里 offer,五轮面试,六个小时灵魂拷问
Choreographer全解析
[code analysis (2)] communication efficient learning of deep networks from decentralized data
Small case of web login (including verification code login)
Using Jupiter notebook in virtual environment
Special test 05 · double integral [Li Yanfang's whole class]
MySQL [read / write lock + table lock + row lock + mvcc]
JS 力扣刷题 103. 二叉树的锯齿形层序遍历
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
Express②(路由)