c - 用大括号括起来的结构是什么意思?
问题描述
最近,我正在跟踪一些源代码。当我看到以下宏时,我卡住了。
#define tmp(x) \
((void) ((struct { \
_Static_assert(sizeof(x) <= 16, "Err"); \
int dum; \
}){1})
该宏用于检查 x 的大小是否小于 16。
我有两个关于这个宏的问题。
首先,以下格式含义中的struct是什么,这种风格有没有关键字?
(struct {
.......
}){1};
其次,为什么要把 assert 放在 struct 里面?为什么不直接调用_Static_assert
main 呢?
解决方案
这种“带括号的声明和初始化”被称为复合文字。这个语法特性首先出现在 C99 中。它的基本语法是:
( type ) { initializer-list }
因此,如果我们省略_Static_cast
, is (struct { int dum; }){1}
,那么您在这里所拥有的,它声明了一个未命名的结构并创建了一个包含该结构的复合文字dum==1
。类型转换为void
简单地丢弃值,避免可能的“未使用值”警告。
其次,为什么要把 assert 放在 struct 里面?为什么不直接调用
_Static_assert
main 呢?
如果您注意到,这会扩展为不以分号结尾的内容;
。(我认为缺少右括号)
是您的复制粘贴错误。)因此它可以用作子表达式,例如
#define tmp(x) \
((void) ((struct { \
_Static_assert(sizeof(x) <= 16, "Err"); \
int dum; \
}){1}))
#include <stdio.h>
int main()
{
const int x=324;
const double y=(tmp(x), 23.342);
printf("%g\n", y);
}
不知道为什么会想要这个,但至少这个宏的作者在某些情况下需要它时可以使用它。
推荐阅读
- javascript - 如果道具是空的
- python - 如何创建一个由来自两条不同数据线的数据组成的整数?
- python - 拆分数据框的每一行并转换为 excel 文件 - 'list' 对象没有属性 'to_frame error'
- cmd - 使用 cmd /c "echo" 时对双引号的奇怪处理
- apache-beam - 如何展平嵌套的 PCollection?
- flutter - 如何将货币符号向右对齐
- java - 基于 XML 上下文的 bean 无法执行构造函数注入:未找到默认构造函数
- python - 使用 Keras 回归或我应该使用什么模型?
- php - 升级到 Laravel 6.0.x 后调用未定义函数 App\Providers\studly_case()
- r - 在字符串的开头移动字符,在 R 中保持其他字符的位置不变