`的typedef结构{foo的酒吧INT}合法性;` [英] Legality of `typedef struct foo {int bar};`
问题描述
此问题是由问题是一个结构{。 ..};一个类型或者一个未命名的变量?
在这个问题上,则OP询问
In that question, the OP asked about
typedef struct student_s {
char* name;
int age;
double height;
struct student_s* next;
};
我问上面的合法性。是诊断必需的,当包含上述code编译(或者更简单, typedef结构{foo的酒吧INT;};
)?
我的看法是,它是合法的,那诊断是不是从语言的角度律师的要求。 (旁白:。我不提倡使用这种这是非常值得的诊断的我就非常希望编译器警告我,如果我误写成code像上面的)
My take is that it is legal, that a diagnostic is not required from a language lawyer perspective. (Aside: I am not advocating using this. It is very worthy of a diagnostic. I would very much want the compiler to warn me if I mistakenly wrote code like the above.)
结果
在C11标准第6.7规定的声明的语法:声明-符初始化说明符列表选择 的;
注意的的init声明符列表的是可选的。这可能会导致一个认为的typedef INT;
是有效的。这不是因为标准还表示,
Section 6.7 of the C11 standard dictates the syntax of a declaration: declaration-specifiers init-declarator-listopt ;
Note that the init-declarator-list is optional. This might lead one to think that typedef int;
is valid. It isn't because the standard also says that
不是static_assert声明等的声明,应至少声明一个声明符,标签或枚举的成员(不是函数的参数或结构或联合的成员除外)。
A declaration other than a static_assert declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration.
因此,的typedef INT;
和 typedef结构{INT巴};
是非法的,因为他们不声明说明符,标签,或枚举的成员。
Thus typedef int;
and typedef struct {int bar};
are illegal because they do not declare a declarator, a tag, or a member of an enumeration.
在另一方面,它在我看来, typedef结构{foo的酒吧INT;};
是合法的,因为它确实申报的东西。特别是,它声明并定义了结构
标记富
。
On the other hand, it appears to me that typedef struct foo {int bar;};
is legal because it does declare something. In particular, it declares and defines the struct
tag foo
.
时的上述推论是否正确?
Is the above reasoning correct?
推荐答案
6.7.1存储类声明定义的typedef
作为存储类说明与以下注释:的类型定义符被称为'存储类说明'句法方便
只; 的
6.7.1 Storage class specifiers defines typedef
as a storage class specifier with the following comment : The typedef specifier is called a ‘‘storage-class specifier’’ for syntactic convenience
only;
和6.7宣言包括:
语法:
- 声明:
- 声明-符初始化说明符列表选择
- declaration:
- declaration-specifiers init-declarator-listopt
- 存储类说明符声明 - 符选择
- 类型说明符声明 - 符选择
- 类型限定符声明 - 符选择
- 函数说明符的声明 - 符选择
- storage-class-specifier declaration-specifiersopt
- type-specifier declaration-specifiersopt
- type-qualifier declaration-specifiersopt
- function-specifier declaration-specifiersopt
- 的init声明符
- 的init声明符列表时,init声明符
- 声明符
- 声明符=初始
第一的约束的是:的声明,应至少声明说明符
(除一个函数的参数或
结构或联合的枚举的成员),标签或成员的由于在init声明符列表是可选的声明,提供了一个声明符或标记声明,我要说的是
typedef结构foo的{INT酒吧;};
是分解为的声明存储类说明符类型说明符
没有初始化声明符。但作为类型说明符内部声明了一个标记(富
)的约束尊重。As the init-declarator-list is optional in a declaration, provided a declarator or tag is declared, I would say that
typedef struct foo {int bar;};
is a declaration decomposed asstorage-class-specifier type-specifier
with no init-declarator. But as the type-specifier declares internally a tag (foo
) the constraint is respected.我的结论是,这种结构是有效的,并仅产生在编译器的警告。
My conclusion is that such a construct is valid and only produces warning on compilers.
这篇关于`的typedef结构{foo的酒吧INT}合法性;`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!