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

【宏定义】——编译时校验

文章目录

  • 编译时校验
    • 功能描述
    • 代码实现
    • 示例代码
      • 正常编译示例
      • 编译错误示例
      • 预处理之后的结果
    • 代码解析
      • `!!e`
      • `struct {int:-!!(e); }`
      • `sizeof(struct {int:-!!(e); })`
    • 参考代码

编译时校验

功能描述

用于在编译时检查一个条件是否为真,如果条件为真则会编译失败,编译器报错

反之如果条件为假,则编译正常,且有返回值,返回 0。

代码实现

/** Force a compilation error if condition is true, but also produce a* result (of value 0 and type int), so the expression can be used* e.g. in a structure initializer (or where-ever else comma expressions* aren't permitted).*/
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))

参数 e 表示用于判断的表达式

示例代码

正常编译示例

#include <stdio.h>#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))int main(void)
{printf("Compilation successful %d.\n", BUILD_BUG_ON_ZERO(0));return 0;
}

结果打印

Compilation successful 0.

编译错误示例

#include <stdio.h>#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))int main(void)
{printf("Compilation successful %d.\n", BUILD_BUG_ON_ZERO(1));return 0;
}

编译错误信息

test.c: In function ‘main’:
test.c:3:51: error: negative width in bit-field ‘<anonymous>3 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))|                                                   ^
test.c:7:44: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’7 |     printf("Compilation successful %d.\n", BUILD_BUG_ON_ZERO(1));

预处理之后的结果

gcc -E test.c -> test.i
int main(void)
{printf("Compilation successful %d.\n", ((int)(sizeof(struct { int:(-!!(1)); }))));return 0;
}

代码解析

!!e

对条件 e 进行两次逻辑非运算,得到 逻辑值 结果 0 或者 1。如果表达式 e 的结果为 0 ,则得到 0 , 如果为非 0 值,则得到 1 。

struct {int:-!!(e); }

如果表达式 e 的结果为 0,则得到结构体 struct {int:0;},这是一个匿名的位域结构体,位域宽度为 0。

如果表达式 e 的结果为 1,则得到结构体 struct {int:-1;},则编译错误。由于位域的宽度不能是负的,所以编译错误,提示错误 error: negative width in bit-field <anonymous>

sizeof(struct {int:-!!(e); })

如果表达式 e 的结果为 0,则使用 sizeof 运算符计算得到这个匿名结构体 struct {int:0;} 的大小为 0,宏的返回值为 0。

参考代码

  • https://blog.csdn.net/u012028275/article/details/127478561
http://www.lryc.cn/news/99210.html

相关文章:

  • C#学习系列之System.Windows.Data Error: 40报错
  • 【java安全】RMI
  • rcu链表综合实践
  • odoo16-python框架-动作
  • 微信小程序——同一控件的点击与长按事件共存的解决方案
  • selenium自动化-获取元素属性信息
  • LabVIEW开发小型减阻试验平台
  • 解决分类任务中数据倾斜问题
  • Vue3 word如何转成pdf代码实现
  • fpga--流水灯
  • 51单片机:数码管和矩阵按键
  • Django + Xadmin 数据列表复选框显示为空,怎么修复这个问题?
  • 《向量数据库指南》——Milvus Cloud2.2.12 易用性,可视化,自动化大幅提升
  • Python web实战 | 用 Flask 框架快速构建 Web 应用【实战】
  • 十、数据结构——链式队列
  • Improving Cross-Modal Retrieval with Set of Diverse Embeddings
  • 物联网阀控水表计量准确度如何?
  • 【C语言数据结构】模拟·顺序表·总项目实现
  • 自然语言处理从入门到应用——LangChain:模型(Models)-[文本嵌入模型Ⅰ]
  • 使用Gradio构建生成式AI应用程序; Stability AI推出Stable Diffusion XL 1.0
  • Java 递归计算斐波那契数列指定位置上的数字
  • ai数字人透明屏的应用场景有哪些?
  • 一、1、Hadoop的安装与环境配置
  • 剑指YOLOv7改进最新MPDIoU损失函数(23年7月首发论文):论文实测YOLOv7模型涨点,超越现有多种G/D/C/EIoU,高效准确的边界框回归的损失
  • 前端JavaScript面试100问(上)
  • C语言第九课------------------数组----------------C中之将
  • MySQL的安装
  • 在Chrome(谷歌浏览器)中安装Vue.js devtools开发者工具及解决Vue.js not detected报错
  • 用Python实现概率矩阵分解(PMF)算法在MovieLens ml-100k数据集上构建精确的推荐系统:深入理解GroupLens数据的操作
  • WPF icon的设置