码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 宏定义_可变参数


    0. Ref

    (19条消息) 整理:C/C++可变参数,“## VA_ARGS”宏的介绍和使用_bat67的博客-CSDN博客

    Replacing text macros - cppreference.com

    Variadic arguments - cppreference.com

    【Just For Fun】C - 可变参数函数、可变参数宏 VA_ARGS、额外的逗号 - 知乎 (zhihu.com)

    define (wengsht.github.io)

    可变参数宏_百度百科 (baidu.com)

    (19条消息) C语言可变参数宏定义方法_gcc宏可变参数_kerneler_的博客-CSDN博客

    Variadic Macros (Using the GNU Compiler Collection (GCC))

    1.可变参数 宏定义

    从C++11标准开始,支持可变参数宏定义。如下所示

    #define identifier replacement-list  (optional)							  (1)	
    #define identifier (parameters ) replacement-list (optional)				(2)	
    #define identifier (parameters , ...) replacement-list (optional)		    (3)	(since C++11)
    #define identifier (...) replacement-list (optional)					   (4) (since C++11)
    #undef identifier	 													 (5)	
    
    • 1
    • 2
    • 3
    • 4
    • 5

    上述代码中,(3)和(4) 是否一样呢?写个代码,测试一下。

    1.1 (3) demo

    #include 
    
    #define debugInfo(args, ...) {printf(args, ##__VA_ARGS__);}
    int main(void)
    {
        debugInfo("Hello, world\n" "File: %s, [func] %s, [line] %d\n", __FILE__, __func__, __LINE__);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    使用以下命令,展开上述代码中宏定义信息

    $g++ variMacro.cpp -E -C -o variMacro.i -std=c++11
    
    • 1

    宏定义“debugInfo"展开后,如下所示:

    # 4 "variMacro.cpp"
    int main(void)
    {
        {printf("Hello, world\n" "File: %s, [func] %s, [line] %d\n", "variMacro.cpp", __func__, 6);};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    编译文件

    $ g++ variMacro.cpp -o variMacro.out -std=c++11
    
    • 1

    执行,运行结果如下:

    $ ./variMacro.out
    Hello, world
    File: variMacro.cpp, [func] main, [line] 6
    
    • 1
    • 2
    • 3

    1.2 (4) demo

    #include 
    
    #define debugInfo(...) {printf(__VA_ARGS__);}
    int main(void)
    {
        debugInfo("Hello, world\n" "File: %s, [func] %s, [line] %d\n", __FILE__, __func__, __LINE__);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    先运行以下命令,展开宏定义参数

    $ g++ variMacro.cpp -E -C -o variMacro.i -std=c++11
    
    • 1

    查看variMacro.i文件,宏定义参数展开如下:

    # 4 "variMacro.cpp"
    int main(void)
    {
        {printf("Hello, world\n" "File: %s, [func] %s, [line] %d\n", "variMacro.cpp", __func__, 6);};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    编译文件

    $ g++ variMacro.cpp -o variMacro.out -std=c++11
    
    • 1

    执行,运行结果如下:

    $ ./variMacro.out
    Hello, world
    File: variMacro.cpp, [func] main, [line] 6
    
    • 1
    • 2
    • 3

    由上述测试结果可见,(3)和(4) 用法不同,效果相同。

    1.3 ##是什么作用?

    在宏定义语法中,表示字符连接符的含义,在这里的作用是什么?

    测试代码如下:输出参数只有一个

    #include 
    
    #define debugInfo(args, ...) {printf(args, __VA_ARGS__);}
    int main(void)
    {
        debugInfo("Hello, world\n");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    使用如下命令,展开宏定义代码:

    $ g++ variMacro.cpp -E -C -o variMacro.i -std=c++11
    
    • 1

    结果如下:

    # 4 "variMacro.cpp"
    int main(void)
    {
        {printf("Hello, world\n", );};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    编译代码,出现以下错误:

    $ g++ variMacro.cpp -o variMacro.out -std=c++11
    variMacro.cpp: In function ‘int main()’:
    variMacro.cpp:3:55: error: expected primary-expression before ‘)’ token
     #define debugInfo(args, ...) {printf(args, __VA_ARGS__);}
                                                           ^
    variMacro.cpp:6:5: note: in expansion of macro ‘debugInfo’
         debugInfo("Hello, world\n");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    修改源代码, 加入“##”

    #include 
    
    #define debugInfo(args, ...) {printf(args, ##__VA_ARGS__);}
    int main(void)
    {
        debugInfo("Hello, world\n");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    使用如下命令,展开宏定义代码:

    $ g++ variMacro.cpp -E -C -o variMacro.i -std=c++11
    
    • 1

    打开variMcro.i文件,结果如下:

    # 4 "variMacro.cpp"
    int main(void)
    {
        {printf("Hello, world\n");};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    编译, 运行结果如下:

    $ g++ variMacro.cpp -o variMacro.out -std=c++11
    $:~/testWorkSpace/args$ ./variMacro.out
    Hello, world
    
    • 1
    • 2
    • 3
  • 相关阅读:
    javaweb学生竞赛管理系统
    测试工程师应具备何种心态?
    [NeurIPS2021] Deep Residual Learning in Spiking Neural Networks【文献精读、翻译】
    STM32初学-外部RTC时钟芯片DS3231
    Python汽车销售系统的设计与实现毕业设计-附源码191807
    egg.js sequelize数据库操作配置
    C++标准模板(STL)- 类型支持 (属性查询,获取类型的对齐要求)
    定制AI问答机器人前需要准备什么数据来训练AI模型?
    [Leetcode] 0058. 最后一个单词的长度
    Mac 执行报错 -bash: mono: command not found 解决方式
  • 原文地址:https://blog.csdn.net/lkzp12/article/details/130855948
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号