如何获取C编译器#Error如果大小(struct ..)不等于给定的数字
How to get C compiler #error if a sizeof(struct ...) not equal to a given number?
如何获得c compile time#如果sizeof(struct ...)不等于给定的数字?
问题来自编程课程,我想避免运行错过的二进制代码。
(我们知道的尺寸运算符在#if .. #endif指令中不起作用。)
如何获得c编译时间#eRROR如果大小(struct ...)不等于给定的数字?
您不能,因为预处理器对类型的大小一无所知。
但是,您可以static_assert
:
static_assert(sizeof(T) == N, "T must have size N")
在C中,关键字是_Static_assert
,也可以通过<assert.h>
中的Macro static_assert
获得。
不要。您已经解释了原因。
在现代C 中,您可以写:
static_assert(sizeof(T) == 42);
最好编写不关心的代码 T
的大小是什么。
#include <assert.h>
//T should have size 10
static_assert(sizeof(T) == 10)
只有最新的C编译器
相关文章:
- 为什么 std::round(sin(pi/6)) 不等于 1?
- 你怎么编码,如果x不等于一个数字,程序就会退出
- u张量模型输出不等于预期输出
- 虽然不等于陈述和/或差异
- 这种关系不等于定义良好的表达式吗?
- OpenGL 版本不等于支持的着色器版本
- 两个 4 位位字段加起来不等于一个字节的大小 - 如何修复?
- 如果整数不等于 0,则将其减小为 1
- C 循环虽然不等于任何两个值
- vb.net单不等于c float
- 如何获取C编译器#Error如果大小(struct ..)不等于给定的数字
- C++ 而语句不等于检查
- 有没有办法检查int是否不等于数组中的所有值?(C )
- 整数数组中的元素数量不等于其大小
- 获取不等于特定值的向量的第一个N元素
- 使用 boost 解析符号链接时,结果不等于原始路径名
- 什么是有效的检查:等于或不等于
- 发送的字节不等于C 套接字PRG中的接收字节
- 奇怪的不等式 - WC_BUTTONW不等于 L "Button" ?
- 为什么向导Wiz0;不等于向导Wiz0();在我的代码中?C2228错误