当前位置: 首页 > news >正文

Advanced Macro Techniques in C/C++: `#`, `##`, and Variadic Macros

Advanced Macro Techniques in C/C++: #, ##, and Variadic Macros

文章目录

  • Advanced Macro Techniques in C/C++: `#`, `##`, and Variadic Macros
    • Illustrative Examples of Macros Using `#` and `##`
      • Stringification Example
      • Token Concatenation Example
      • Nested Macros Example
    • Key Concepts of Nested Macros
      • Macro Expansion Rules
      • Operators in Macro Definitions
      • Challenges with `#` and `##`
    • Detailed Example
    • Example of Nested Macro Expansion
    • Direct Expansion Example
    • Why Use Nested Macros?
    • Variable Argument Macros (Variadic Macros)
      • Explanation
    • Summary

DateAuthorVersionNote
2024-12-02TaoV1.0Finish the document.

Macros are an integral component of the C/C++ preprocessor, enabling the definition of reusable code fragments that enhance flexibility and abstraction. The # and ## operators are powerful tools used within macros for stringification and token concatenation, respectively. A deep understanding of these operators, particularly in nested contexts, allows for more sophisticated and reliable macro behavior.

The use of nested macros is a common technique in C and C++ to perform operations like token stringification or concatenation effectively through preprocessor directives. Mastering the behavior of macros when utilizing # (stringification) or ## (token concatenation) operators ensures that arguments are appropriately expanded, even when these arguments themselves involve other macro definitions.

Illustrative Examples of Macros Using # and ##

Stringification Example

#define Stringify(A) #A
printf("%s\n", Stringify(Hello)); // Output: "Hello"

Here, the # operator is used to convert the argument Hello into a string literal.

Token Concatenation Example

#define Concat(A, B) A##B
int HelloWorld = 5;
printf("%d\n", Concat(Hello, World)); // Output: 5

The ## operator concatenates Hello and World into a single token, HelloWorld.

Nested Macros Example

#define Outer(A) Inner(A)
#define Inner(A) #A
printf("%s\n", Outer(Hello)); // Output: "Hello"

By using nested macros, the argument is fully expanded before the stringification operation is applied.

Key Concepts of Nested Macros

Macro Expansion Rules

  • Macros in C are expanded in a single pass unless explicitly nested within other macros.
  • If a macro’s argument includes another macro, the preprocessor does not expand the inner macro unless explicitly instructed through an additional macro layer.

Operators in Macro Definitions

  • #: Converts a macro argument into a string literal.
  • ##: Concatenates two tokens into one.

Challenges with # and ##

When using # or ## within a macro, the preprocessor suppresses the evaluation of macro arguments before applying the operator. This suppression ensures that # or ## acts on the exact tokens provided, requiring an additional layer of macro indirection to achieve the desired behavior.

Detailed Example

Consider the following macro definitions:

#define Stringify(A) _Stringify(A)        // Outer macro
#define _Stringify(A) #A                 // Inner macro that performs stringification#define Concat(A, B) _Concat(A, B)       // Outer macro
#define _Concat(A, B) A##B               // Inner macro that performs concatenation
  • Stringify Macro:

    • Stringify(A) passes its argument A to _Stringify(A).
    • _Stringify(A) then applies the # operator to convert A into a string literal.
  • Concat Macro:

    • Concat(A, B) passes its arguments A and B to _Concat(A, B).
    • _Concat(A, B) uses the ## operator to concatenate A and B.

Example of Nested Macro Expansion

Consider the following code:

printf("%s\n", Stringify(Concat(Hel, lo)));
  • Step 1: Expand Stringify(Concat(Hel, lo)):

    • Stringify(A) becomes _Stringify(A), where A is Concat(Hel, lo).
    • Result: _Stringify(Concat(Hel, lo)).
  • Step 2: Expand _Stringify(Concat(Hel, lo)):

    • Before applying the # operator, the macro argument Concat(Hel, lo) is expanded because it is passed through another macro layer.
    • Concat(Hel, lo) expands to _Concat(Hel, lo) and subsequently to Hello (using the ## operator).
    • Result: _Stringify(Hello).
  • Step 3: Apply _Stringify(Hello):

    • The # operator converts Hello into a string literal.
    • Result: "Hello".

Output:

Hello

Direct Expansion Example

Consider the following code:

printf("%s\n", _Stringify(Concat(Hel, lo)));
  • Step 1: Expand _Stringify(Concat(Hel, lo)):
    • _Stringify(A) directly applies the # operator to the argument Concat(Hel, lo) without expanding it.
    • Result: "Concat(Hel, lo)".

Output:

Concat(Hel, lo)

Why Use Nested Macros?

Nested macros ensure proper argument expansion before applying the # or ## operators. Without this nesting:

  • The # operator would stringify the literal macro argument without expanding it.
  • The ## operator would concatenate the original tokens rather than their expanded forms.

Variable Argument Macros (Variadic Macros)

Variadic macros allow for defining macros that accept a variable number of arguments. This feature is particularly advantageous for flexible logging or debugging macros.

Consider the following example:

#define Log(format, ...) printf(format, ##__VA_ARGS__)

Explanation

  • Log(format, ...) defines a macro that accepts a format string and a variable number of additional arguments.
  • __VA_ARGS__ is a special placeholder for the variable arguments.
  • ##__VA_ARGS__ is used to handle the case where no additional arguments are provided, preventing a trailing comma error.

Usage Example:

Log("Error: %s, Code: %d\n", "File not found", 404); // Output: Error: File not found, Code: 404
Log("Simple message\n"); // Output: Simple message

Summary

  • Utilize nested macros when dealing with macro arguments involving other macros and the # or ## operators.
  • The outer macro ensures arguments are fully expanded before being processed by the inner macro.
  • Variadic macros (... and __VA_ARGS__) provide the ability to create macros that accommodate a variable number of arguments, enhancing code flexibility and readability.

These examples illustrate the critical importance of proper macro usage:

  1. With Nested Macros: Stringify(Concat(Hel, lo)) correctly expands to "Hello".
  2. Without Nested Macros: _Stringify(Concat(Hel, lo)) results in "Concat(Hel, lo)" due to the lack of proper expansion.
  3. Variadic Macros: Provide a powerful mechanism for managing functions like logging without manual adjustments for the number of arguments.

相关文章:

Advanced Macro Techniques in C/C++: `#`, `##`, and Variadic Macros

Advanced Macro Techniques in C/C: #, ##, and Variadic Macros 文章目录 Advanced Macro Techniques in C/C: #, ##, and Variadic MacrosIllustrative Examples of Macros Using # and ##Stringification ExampleToken Concatenation ExampleNested Macros Example Key Conc…...

Maven、JAVAWeb、Servlet

知识点目标 1、MavenMaven是什么Maven项目的目录结构Maven的Pom文件Maven的命令Maven依赖管理Maven仓库JavaWeb项目 2.网络基础知识 3、ServletMaven Maven是什么 Maven是Java的项目管理工具,可以构建,打包,部署项目,还可以管理…...

分布式资源调度——yarn 概述(资源调度基本架构和高可用的实现)

此文章是学习笔记,图片均来源于B站:哈喽鹏程 yarn详细介绍 1、yarn 简介1.1 yarn的简介1.2 yarn 的基本架构1.3. yarn 的高可用 2、yarn 调度策略、运维、监控2.1 yarn 的调度策略2.1.1 FIFO scheduler(先进先出)2.1.2 容量调度2.1.3 公平调度 2.2 yarn…...

网页开发的http基础知识

请求方式-GET:请求参数在请求行中,没有请求体,如:/brand/findAll?nameoPPo&status1。GET请求大小在浏览器中是有限制的请求方式-POST:请求参数在请求体中,POST请求大小是没有限制的 HTTP请求&#xf…...

学习方法的进一步迭代————4

今天又在怀疑第二大脑的可靠程度 为什么呢? 还是因为自己没记住东西,感觉没学到东西。 其实自己知道大脑本就不应该用来存放知识而是用来思考知识,但是自己还是陷在里面了,我觉得其本质不是因为认知还不够,也不是因为还有点不适…...

数据科学家创建识别假图像的工具

Pixelator v2 是一款用于识别假图像的工具。它采用了全新的图像真实性技术组合,其能力超出了人眼所能看到的范围。 它能够以比传统方法更高的准确度识别图像中的细微差异,并且已被证明能够检测到小至 1 个像素的交替。 使用 SSIM 和 Pixelator v2 突出显…...

使用 GORM 与 MySQL 数据库进行交互来实现增删改查(CRUD)操作

1、安装 GORM 和 MySQL 驱动 新版本库是gorm.io/gorm go get -u gorm.io/gormgo get -u gorm.io/driver/mysql2、连接 MySQL 数据库 package mainimport ("gorm.io/driver/mysql""gorm.io/gorm""log" )func main() {// 数据源名称 (DSN) 格式&a…...

Day2 生信新手笔记: Linux基础

一、基础知识 1.1 服务器 super computer 或 server 1.2 组学数据分析 组学数据:如基因组学、转录组学、蛋白质组学等; 上游分析:主要涉及原始数据的获取和初步处理,计算量大,消耗的资源较多,在服务器完…...

001集—— 创建一个WPF项目 ——WPF应用程序入门 C#

本例为一个WPF应用&#xff08;.NET FrameWork&#xff09;。 首先创建一个项目 双击xaml文件 双击xaml文件进入如下界面&#xff0c;开始编写代码。 效果如下&#xff1a; 付代码&#xff1a; <Window x:Class"WpfDemoFW.MainWindow"xmlns"http://schema…...

【C++】1___引用

一、基本语法 数据类型 &别名 原名 #include<iostream> using namespace std; int main(){int a 10;int &b a;cout<<"a"<<a<<endl; // a10cout<<"b"<<b<<endl;// a10b 20;cout<<"a…...

如何通过 JWT 来解决登录认证问题

1. 问题引入 在登录功能的实现中 传统思路&#xff1a; 登录页面时把用户名和密码提交给服务器服务器验证用户名和密码&#xff0c;并把检验结果返回给后端如果密码正确&#xff0c;则在服务器端创建 session&#xff0c;通过 cookie 把 session id 返回给浏览器 但是正常情…...

高效集成:将聚水潭数据导入MySQL的实战案例

聚水潭数据集成到MySQL&#xff1a;店铺信息查询案例分享 在数据驱动的业务环境中&#xff0c;如何高效、准确地实现跨平台的数据集成是每个企业面临的重要挑战。本文将聚焦于一个具体的系统对接集成案例——将聚水潭的店铺信息查询结果集成到MySQL数据库中&#xff0c;以供BI…...

Jenkins-基于 JNLP协议的 Java Web 启动代理

在上一篇的基础配置上进行以下步骤 工作流程&#xff1a; 通过 JNLP 启动代理&#xff0c;客户端从 Jenkins 服务器上下载一个 agent.jar 文件。该文件启动时&#xff0c;代理程序通过 JNLP 协议连接到 Jenkins 主节点。一旦连接成功&#xff0c;代理节点就可以执行从主节点分…...

Qt数据库操作-QSqlQueryModel 的使用

QSqlQueryModel 功能概述 QSqlQueryModel 是 QSqlTableModel 的父类。QSqlQueryModel 封装了执行 SELECT 语句从数据库查询数据的功能&#xff0c;但是 QSqlQueryModel 只能作为只读数据源使用&#xff0c;不可以编辑数据。QSqlQueryModel 类的主要函数如下&#xff1a; 接口…...

C语言编程1.21波兰国旗问题

题目描述 桌上有 n ( 1 < n < 10000 ) 面小旗&#xff0c;一部分是白旗&#xff0c;一部分是红旗(波兰国旗由白色和红色组成)。唯一允许的操作是交换两面小旗位置。请你设计一个算法&#xff0c;用最少的交换操作将所有的白旗都置于红旗的之前。 输入格式 第一行为一个…...

如何利用微型5G网关为智慧无人矿车提供精确定位

随着5G、AI、物联网技术的发展和普及&#xff0c;越来越多行业正在加快生产、运营、管理的无人化、数字化与智能化&#xff0c;以适应当前我国“智慧、绿色、低碳”的新型发展模式需要。其中矿产业就是典型场景之一。针对矿山场景的智慧化、无人化转型&#xff0c;佰马提供基于…...

使用docker-compese部署SFTPGo详解

官网&#xff1a;SFTP & FTP as a Managed Service (SaaS) and On-premise 一、SFTPGo简介 SFTPGo 是一款功能强大的文件传输服务器软件。它支持多种协议&#xff08;SFTP、SCP、FTP/S、WebDAV、HTTP/S&#xff09;和多个存储后端。 借助 SFTPGo&#xff0c;您可以利用本地…...

Ajax基础总结(思维导图+二维表)

一些话 刚开始学习Ajax的时候&#xff0c;感觉很模糊&#xff0c;但是好像学什么都是这样的&#xff0c;很正常&#xff0c;但是当你学习的时候要持续性敲代码&#xff0c;边敲代码其实就可以理解很多了。然后在最后的总结&#xff0c;其实做二维表之后&#xff0c;就可以区分…...

Spring Task和WebSocket使用

在现代 Web 应用中&#xff0c;WebSocket 作为一种全双工通信协议&#xff0c;为实时数据传输提供了强大的支持。若要确保 WebSocket 在生产环境中的稳定性和性能&#xff0c;使用 Nginx 作为反向代理服务器是一个明智的选择。本篇文章将带你了解如何在 Nginx 中配置 WebSocket…...

微信小程序 本地调试和vconsole可以 但在体验上页面不请求数据

微信小程序页面不请求数据 本地调试和vconsole可以 但在体验版页面不请求数据&#xff0c;如遇到这类问题基本都是一样的解决办法 1、如何调试找到问题 首先要把小程序本地设置的不校验合法域名关掉&#xff0c;不然本地一直都是好的 然后通过本地真机调试打断点找到问题位…...

深度学习在微纳光子学中的应用

深度学习在微纳光子学中的主要应用方向 深度学习与微纳光子学的结合主要集中在以下几个方向&#xff1a; 逆向设计 通过神经网络快速预测微纳结构的光学响应&#xff0c;替代传统耗时的数值模拟方法。例如设计超表面、光子晶体等结构。 特征提取与优化 从复杂的光学数据中自…...

设计模式和设计原则回顾

设计模式和设计原则回顾 23种设计模式是设计原则的完美体现,设计原则设计原则是设计模式的理论基石, 设计模式 在经典的设计模式分类中(如《设计模式:可复用面向对象软件的基础》一书中),总共有23种设计模式,分为三大类: 一、创建型模式(5种) 1. 单例模式(Sing…...

linux之kylin系统nginx的安装

一、nginx的作用 1.可做高性能的web服务器 直接处理静态资源&#xff08;HTML/CSS/图片等&#xff09;&#xff0c;响应速度远超传统服务器类似apache支持高并发连接 2.反向代理服务器 隐藏后端服务器IP地址&#xff0c;提高安全性 3.负载均衡服务器 支持多种策略分发流量…...

Java-41 深入浅出 Spring - 声明式事务的支持 事务配置 XML模式 XML+注解模式

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持续更新中&#xff01;&#xff08;长期更新&#xff09; 目前2025年06月05日更新到&#xff1a; AI炼丹日志-28 - Aud…...

【RockeMQ】第2节|RocketMQ快速实战以及核⼼概念详解(二)

升级Dledger高可用集群 一、主从架构的不足与Dledger的定位 主从架构缺陷 数据备份依赖Slave节点&#xff0c;但无自动故障转移能力&#xff0c;Master宕机后需人工切换&#xff0c;期间消息可能无法读取。Slave仅存储数据&#xff0c;无法主动升级为Master响应请求&#xff…...

【JavaSE】绘图与事件入门学习笔记

-Java绘图坐标体系 坐标体系-介绍 坐标原点位于左上角&#xff0c;以像素为单位。 在Java坐标系中,第一个是x坐标,表示当前位置为水平方向&#xff0c;距离坐标原点x个像素;第二个是y坐标&#xff0c;表示当前位置为垂直方向&#xff0c;距离坐标原点y个像素。 坐标体系-像素 …...

CSS设置元素的宽度根据其内容自动调整

width: fit-content 是 CSS 中的一个属性值&#xff0c;用于设置元素的宽度根据其内容自动调整&#xff0c;确保宽度刚好容纳内容而不会超出。 效果对比 默认情况&#xff08;width: auto&#xff09;&#xff1a; 块级元素&#xff08;如 <div>&#xff09;会占满父容器…...

【VLNs篇】07:NavRL—在动态环境中学习安全飞行

项目内容论文标题NavRL: 在动态环境中学习安全飞行 (NavRL: Learning Safe Flight in Dynamic Environments)核心问题解决无人机在包含静态和动态障碍物的复杂环境中进行安全、高效自主导航的挑战&#xff0c;克服传统方法和现有强化学习方法的局限性。核心算法基于近端策略优化…...

【MATLAB代码】基于最大相关熵准则(MCC)的三维鲁棒卡尔曼滤波算法(MCC-KF),附源代码|订阅专栏后可直接查看

文章所述的代码实现了基于最大相关熵准则(MCC)的三维鲁棒卡尔曼滤波算法(MCC-KF),针对传感器观测数据中存在的脉冲型异常噪声问题,通过非线性加权机制提升滤波器的抗干扰能力。代码通过对比传统KF与MCC-KF在含异常值场景下的表现,验证了后者在状态估计鲁棒性方面的显著优…...

NPOI操作EXCEL文件 ——CAD C# 二次开发

缺点:dll.版本容易加载错误。CAD加载插件时&#xff0c;没有加载所有类库。插件运行过程中用到某个类库&#xff0c;会从CAD的安装目录找&#xff0c;找不到就报错了。 【方案2】让CAD在加载过程中把类库加载到内存 【方案3】是发现缺少了哪个库&#xff0c;就用插件程序加载进…...