问题描述
带有clang-902.0.39.2的Apple LLVM 9.1.0,使用-std=c11
接受:
Apple LLVM 9.1.0 with clang-902.0.39.2, using -std=c11
, accepts:
typedef struct { int i; float f; } S;
for (S s = { 0, 0 }; s.i < 25; ++s.i, s.f = i/10.f)
…
但拒绝:
for (struct { int i; float f; } s = { 0, 0 }; s.i < 25; ++s.i, s.f = s.i/10.f)
…
显示以下消息:
由于Clang违反了C标准中的某些约束,因此拒绝它是正确的吗?哪个条款和段落?还是Clang错误?
Is Clang correct to reject this, because it violates some constraint in the C standard? Which clause and paragraph? Or is it a Clang bug?
推荐答案
C11:
1语句
for ( clause-1 ; expression-2 ; expression-3 ) statement
表现如下:表达式expression-2是控制表达式,它是 在每次执行循环主体之前进行评估.表达式expression-3是 每次执行循环主体后,将其评估为void表达式.如果第1条是 声明,它声明的任何标识符的范围是声明的其余部分和整个循环,包括其他两个表达式;它是在对控制表达式进行第一次评估之前按执行顺序达到的.如果子句1是表达式,则在对控制表达式进行第一次求值之前,它会被视为空表达式.158)
behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any identifiers it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.158)
剩下的[2],只是谈到处理省略的子句/表达式...
Left out [2], just speaking of handling ommited clauses/expressions...
没有提及任何限制.第二个例子是一个有效的声明–而且我认为clang也不会抱怨以下内容:
Nothing mentioned about any restrictions. So as second example is a valid declaration – and I assume clang does not complain about the following either:
void f(void)
{
struct { int x, y; } point;
// use point...
}
–那么第二个示例应该是有效的C代码,据我所知,clang拒绝是错误的.
– then the second example should be valid C code, so as far as I see, clang is wrong in rejecting.
这篇关于在内部声明结构吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!