重新声明未加标签的结构是否是兼容类型? [英] Is a redeclaration of an untagged structure a compatible type?

查看:46
本文介绍了重新声明未加标签的结构是否是兼容类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于此问题中所述的目的,我们要这样做:

For purposes expressed in this question, we want to do this:

typedef struct { int a; } A;
typedef struct { struct { int a; }; int b; } B;

A *BToA(B *b) { return (A *) b; }
B *AToB(A *a) { return (B *) a; }

愿望是强制类型转换符合C 2011 6.7.2.1 15 表示指向经过适当转换的结构对象的指针指向其初始成员(或者,如果该成员是位字段,则指向它所驻留的单元),反之亦然."

The desire is that the casts conform to C 2011 6.7.2.1 15, which says "A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa."

由于 struct {int a;} B 中没有名字,我们称它为 A'.

Since the struct { int a; } inside B does not have a name, let’s call it A'.

未明确定义适当地".我假设如果 A * 是指向 A'类型的对象的有效指针,那么(A *)b 将执行适当的转换,同样,如果 a 是指向 B 中的 A'的指针,则(B *)a 是合适的转换.

"Suitably" is not explicitly defined. I presume that if A * is a valid pointer to an object of type A', then (A *) b performs a suitable conversion, and, similarly, if a is a pointer to an A' that is in a B, then (B *) a is a suitable conversion.

所以问题是: A * 是指向 A'类型的对象的有效指针吗?

So the question is: Is A * a valid pointer to an object of type A'?

每个 6.7.6.1 如果 A A'兼容,则* A'* 兼容.

Per 6.7.6.1, A * is compatible with A' * if A is compatible with A'.

每个 6.2.7 ,两种类型如果它们的类型相同,则具有兼容的类型…而且,如果在单独的翻译单元中声明的两个结构,联合或枚举类型的标签和成员满足以下要求,则它们是兼容的:如果一个声明有标签,则应声明另一个具有相同的标签.如果两者都在各自翻译单位内的任何位置完成,则适用以下附加要求:成员之间应存在一对一的对应关系,以使每对对应的成员声明为兼容类型;如果该对中的一个成员是使用对齐方式说明符声明的,则另一个成员使用等效的对齐方式说明符声明;如果该对中的一个成员声明了一个名称,则另一个成员则声明了相同的名称.对于两个结构,相应的成员应以相同的顺序声明……"

Per 6.2.7, "Two types have compatible type if their types are the same… Moreover, two structure, union, or enumerated types declared in separate translation units are compatible if their tags and members satisfy the following requirements: If one is declared with a tag, the other shall be declared with the same tag. If both are completed anywhere within their respective translation units, then the following additional requirements apply: there shall be a one-to-one correspondence between their members such that each pair of corresponding members are declared with compatible types; if one member of the pair is declared with an alignment specifier, the other is declared with an equivalent alignment specifier; and if one member of the pair is declared with a name, the other is declared with the same name. For two structures, corresponding members shall be declared in the same order…"

6.7.2.3 5不能是相同类型.:每个不包含标签的结构,联合或枚举类型的声明都声明了不同的类型."

These cannot be the same type by 6.7.2.3 5: "Each declaration of a structure, union, or enumerated type which does not include a tag declares a distinct type."

由于它们不是同一类型,因此它们兼容吗? 6.2.7 中的文本表示,如果兼容,在单独的翻译单元中声明,但它们在同一翻译单元中.

Since they are not the same type, are they compatible? The text in 6.2.7 says they are compatible if declared in separate translation units, but these are in the same translation unit.

推荐答案

正如您在问题中所提出的那样,该标准清楚明确地指出了两个结构定义.} 在同一个翻译单元中声明两种不兼容的类型.尽管这可能是怪异"的事实.编译器始终遵循该标准.

As you laid out in the question, the standard clearly and unambiguously says that two struct definitions struct { int a; } in the same translation unit declare two incompatible types. Notwithstanding the fact that this might be "weird". Compilers have always followed the standard.

在我看来,这是一种合理的行为:如果您碰巧在项目中具有语义上不相关的结构,而这些结构恰好具有相同类型的成员列表,那么您确实希望编译器拒绝这两者之间的偶然分配.

This seems like reasonable behaviour to me: if you happen to have semantically unrelated structs in your project that coincidentally have a member list with the same types, you do want the compiler to reject an accidental assignment between the two.

重新.根据6.7.2.1/13

Re. the code in your question, according to 6.7.2.1/13,

匿名结构或联合的成员被视为包含结构或联合的成员.

The members of an anonymous structure or union are considered to be members of the containing structure or union.

所以我将 B 的定义等同于:

So I would treat the definition of B as being equivalent to:

typedef struct { int a; int b; } B;

出于进一步分析的目的.

for purposes of further analysis.

这篇关于重新声明未加标签的结构是否是兼容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆