当前位置:网站首页>变长参数__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
边栏推荐
- Kettle--控件解析
- Leetcode? The first common node of two linked lists
- [code analysis (5)] communication efficient learning of deep networks from decentralized data
- How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
- SQL learning | complex query
- Jiannanchun understood the word game
- 联想产品经理林林:天津当地网络运营商网络故障 ZUI系统后台服务器暂时无法正常工作
- Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]
- [code analysis (1)] communication efficient learning of deep networks from decentralized data
- Analysis of redo log generated by select command
猜你喜欢
随机推荐
JS force deduction brush question 103 Zigzag sequence traversal of binary tree
YARN线上动态资源调优
Problems encountered in the project (V) understanding of operating excel interface poi
Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
Choreographer全解析
Analysis of redo log generated by select command
初探 Lambda Powertools TypeScript
Solution of discarding evaluate function in surprise Library
Basic SQL query and learning
VsCode-Go
自动化的艺术
读了一篇博客,重新理解闭包整理一下
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
Function executes only the once function for the first time
Reading notes: fedgnn: Federated graph neural network for privacy preserving recommendation
Android 面试主题集合整理
编程旅行之函数
OSS cloud storage management practice (polite experience)
UML统一建模语言









