了解同一范围内的声明

Understanding declarations in the same scope

这是一个关于 ISO C 的语言律师问题。

我想了解标准中是如何定义声明的。我使用 N1570。考虑以下情况:

案例一.

int a;
extern int a; //compatible types, external linkage, well-defined behavior

案例二.

extern int a;
int a; //well-defined behavior, external linkage, well-defined behavior

案例三.

int a;
static int a; //6.2.2/6.9.2UB, linkage-disagreement

案例4.

static int a;
extern int a; //6̶.̶2̶.̶2̶/̶6̶.̶9̶.̶2̶ ̶U̶B̶,̶ ̶l̶i̶n̶k̶a̶g̶e̶-̶d̶i̶s̶a̶g̶r̶e̶e̶m̶e̶n̶t̶
              //as @EricPostpischil mentioned in the comment
              //it is well-defined in 6.2.2

案例5.

int a;
long a; //6.7.2 UB incompatible types

案例6.

int a;
const int a; //6.7.2/6.7.3 incompatible types, different qualifiers

案例7.

enum{
    a
};

enum{
    a //UB, why?
};

案例8.

 enum {
     a
 };

 const unsigned char a; //UB, why?

案例1-4

标准中的解释清晰明确。

6.2.2(p7):

If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.

案例5

6.2.7(p1):

部分进行了解释

If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.

6.7(p4):

All declarations in the same scope that refer to the same object or function shall specify compatible types.

案例6

6.7.3(p10)解释:

For two qualified types to be compatible, both shall have the identically qualified version of a compatible type

案例7-8

不清楚。我没有在标准中找到与它们相关的任何正式参考。 6.2.7(p1) 状态:

For two enumerations, corresponding members shall have the same values.

案例 7 满足该要求。

所以我没有发现任何问题。

我没有发现任何与案例 8 明确相关的内容,所以好像它没有在标准中定义,它应该是 UB。

你能帮忙在案例7的标准中找到解释吗案例8?

案例7

由6.7.2.3第1、4、5段(第137页)解释(重点是我的)

1 A specific type shall have its content defined at most once.

4 All declarations of structure, union, or enumerated types that have the same scope and use the same tag declare the same type. Irrespective of whether there is a tag or what other declarations of the type are in the same translation unit, the type is incomplete [footnote 129)] until immediately after the closing brace of the list defining the content, and complete thereafter.

5 Two declarations of structure, union, or enumerated types which are in different scopes or use different tags declare distinct types. Each declaration of a structure, union, or enumerated type which does not include a tag declares a distinct type.

因此,相同类型枚举的示例 [如果不是第 1 段] 就像

enum TagNameA
{
    a
};
enum TagNameA
{
   a
};

案例8 由6.7.2.2第3段(第136页)解释(重点是我的)

The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted [footnote: 127)]

...

[footnote 127)] Thus, the identifiers of enumeration constants declared in the same scope shall all be distinct from each other and from other identifiers declared in ordinary declarators.

案例 8

const unsigned char a;

a普通声明符,与枚举常量标识符不同 =37=] a.

案例 8

 enum {
     a
 };

 const unsigned char a; //UB, why?

这不是UB。这是语义错误。 UB 可能只在运行时发生。

重复标识符 'a'。考虑

int b = a;  //which a? 

案例 7

enum{
    a
};

enum{
    a //UB, why?
};

这不是UB。这是语义错误。 UB 可能只在运行时发生。

重复标识符 'a'。考虑

int b = a;  //which a?