In C11 it is legal to write, for instance:
例如,在C11中,可以合法地写:
int b = (some_function_returning_void(), 1020);
And you'll get back 1020. But it won't let you write:
你会得到1020美元。但它不会让你写下:
int b = (_Static_assert(2 > 1, "all is lost"), 304);
gcc returning
GCC返回
error: expected expression before '_Static_assert'
And it would be inconvenient to use _Static_assert
outside of an expression sometimes since you're out of luck with preprocessor macros-based pseudo-functions that verify their arguments then.
有时在表达式之外使用_STATIC_ASSERT会很不方便,因为您不太可能使用基于预处理器宏的伪函数来验证它们的参数。
更多回答
static_assert
is meant to be usable outside functions, too. Therefore syntactically it can't be an expression. But for my curiosity, I didn't really get why you would need that, and cannot just place it before the declaration.
static_assert也可以在函数外部使用。因此,从语法上讲,它不可能是表达式。但出于好奇,我真的不明白为什么你需要它,而且不能把它放在声明之前。
Do you have a more concrete example of where you would use this? There may be a better way.
你有没有更具体的例子来说明你会把它用在哪里?也许有更好的办法。
my guess is a function-like macro? Within a macro you can use a very crude trick - a bitfield with negative width for example, this will fail at compile time because it doesn't make any sense.
我猜是一个类似函数的宏在一个宏中,你可以使用一个非常粗糙的技巧-例如一个宽度为负的位域,这将在编译时失败,因为它没有任何意义。
I don't quite see why you need this, but you could create a function containing nothing but a static assert. The optimizer will remove it since it contains no run-rime code.
我不太明白您为什么需要这样做,但是您可以创建一个只包含静态断言的函数。优化器将删除它,因为它不包含运行周期代码。
Functions can't access their callee's variables, so i can't assert over anything but global consts this way.
函数不能访问其被调用者的变量,因此我只能以这种方式断言全局常量。
优秀答案推荐
This is doable in ISO C11. The trick is to put _Static_assert
in a struct declaration, in a sizeof expression:
这在ISO C11中是可行的。诀窍是将_STATIC_ASSERT放在一个结构声明中,放在一个sizeof表达式中:
sizeof(struct { _Static_assert(0, "yay it failed"); int dummy; })
The dummy field is necessary because empty struct is a GNU extension
, according to clang -std=c11 -Weverything
.
根据clang-std=c11-WEverything的说法,这个虚拟字段是必需的,因为空结构是GNU扩展。
_Static_assert
is, unfortunately, a special kind of declaration, not a function or an operator. You won't be able to slip it into an expression, unless you use something non-standard. E.g. compiler extensions like GCC's "statement expressions"
遗憾的是,_Static_Assert是一种特殊类型的声明,而不是函数或运算符。除非您使用了非标准的内容,否则您无法将其添加到表达式中。例如像GCC的“语句表达式”这样的编译器扩展
int b = ({ _Static_assert(2 > 1, "all is lost"); 304; });
or
或
int b = (({ _Static_assert(2 > 1, "all is lost"); }), 304);
更多回答
Nice hack. Too bad it turns out gcc thinks const
variables that are assigned constant values aren't in fact constants, so i can't use them in static asserts.
砍得好。遗憾的是,GCC认为赋值为常量的常量变量实际上不是常量,所以我不能在静态断言中使用它们。
@L29Ah: That's not just something "gcc thinks". That's how C language is defined. In C const
variables do not form constant expressions.
@L29Ah:这不仅仅是“GCC认为”的事情。这就是C语言的定义。在C中,const变量不构成常量表达式。
I wonder if it's possible to make a wrapper macro that works in both expression and declaration contexts
我想知道是否有可能制作一个同时在表达式和声明上下文中工作的包装宏
我是一名优秀的程序员,十分优秀!