码农知识堂 - 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
  • 相关阅读:
    软件工程总结
    zernike相衬显微镜、图像矩、zernike多项式和像差
    韩语难学吗
    JAVA并发编程——CAS与AQS源码详解
    Vue3.x新特性 Vue3新功能(详细)
    leetcode 135. 分发糖果
    电子机械节拍器芯片-DLT5F03ATF-杰力科创
    在 ABAP 开发工具运行时错误显示界面里植入思否猫
    python主题建模可视化LDA和T-SNE交互式可视化
    语音信号处理-基础(五):傅立叶变换【离散傅里叶变换(DFT)、O(n^2)】【快速傅里叶变换(FFT)、O(nlogn)】、【短时傅里叶变换(STFT)】
  • 原文地址:https://blog.csdn.net/lkzp12/article/details/130855948
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号