not*_*ser 0 c macros gcc static-analysis
信不信由你,我想static_assert在宏扩展到指定的初始化器:
#define INIT(N) \
/* static_assert((N) < 42, "too large"), */ \
[(N)] = (N)
int array[99] = { INIT(1), INIT(2), INIT(42) };
Run Code Online (Sandbox Code Playgroud)
我想要一个错误INIT(42),但取消注释static_assert是一个语法错误.AFAIK static_assert在语法上是一种声明.我如何在这个例子中使用它?
#define INIT(N) \
[(N)] = (sizeof((struct {_Static_assert((N) < 42, "too large");char c[N];}){{0}}.c))
Run Code Online (Sandbox Code Playgroud)
......我不确定自己是如何结束这种憎恶的.但是嘿,它适用于(for N > 0)!
// A struct declaration is a valid place to put a static_assert
struct {_Static_assert((N) < 42, "too large"); }
// Then we can place that declaration in a compound literal...
(struct {_Static_assert((N) < 42, "too large"); }){ }
// But we can't just throw it away with `,`: that would yield a non-constant expression.
// So let's add an array of size N to the struct...
(struct {_Static_assert((N) < 42, "too large");char c[N];}){{0}}
// And pry N out again through sizeof!
sizeof((struct {_Static_assert((N) < 42, "too large");char c[N];}){{0}}.c)
Run Code Online (Sandbox Code Playgroud)
0友好的版本(只需添加然后减去1所以数组的正大小):
#define INIT(N) \
[(N)] = (sizeof((struct { \
_Static_assert((N) < 42, "too large"); \
char c[(N) + 1]; \
}){{0}}.c) - 1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
213 次 |
| 最近记录: |