秒中有引用. N3797工作草案的3.3.1 / 4:
Given a set of declarations in a single declarative region, each of
which specifies the same unqualified name,— exactly one declaration shall declare a class name or enumeration
name that is not a typedef name and the other declarations shall all
refer to the same variable or enumerator, or all refer to functions
and function templates;
我们可以在一个声明性区域中多次声明枚举类型名称:
enum A; // declared first time, the other declaration with the same
// unqualified name shall all refer to the same variable or enumeration.
enum A; // This and the other declarations shall all refer to the that enumeration
extern int A; // Fail, now enum A is hidden and we can access it
// via elaborated-type-specifier only
我糊涂了.你能解释一下这种行为吗?我想找到对当前工作草案的相应参考.
最佳答案 完整报价n3797 3.3.1 / 4是:
Given a set of declarations in a single declarative region, each of which specifies the same unqualified name,
— they shall all refer to the same entity, or all refer to functions and function templates; or
— exactly one declaration shall declare a class name or enumeration name that is not a typedef name
and the other declarations shall all refer to the same variable or enumerator, or all refer to functions and function templates; in this case the class name or enumeration name is hidden.
你写的代码是错的.有关所需语法,请参见S7.2,其中可能是以下任何一种:
enum A : int; // declaration (and it is an enumeration name)
enum A : int; // refers to the same entity
enum struct A; // declaration (and it is an enumeration name)
enum struct A; // refers to the same entity
enum A {}; // declaration (and it is an enumeration name)
enum A {}; // refers to the same entity
然后,可以在翻译单元中稍后发生以下情况.
extern int A; // declaration hides enumeration name
void f() {
A j = A.a; // illegal. A is hidden
int k = A; // legal
}
见n3797 3.3.10 / 2:
A class name (9.1) or enumeration name (7.2) can be hidden by the name of a variable, data member,
function, or enumerator declared in the same scope. If a class or enumeration name and a variable, data member, function, or enumerator are declared in the same scope (in any order) with the same name, the class or enumeration name is hidden wherever the variable, data member, function, or enumerator name is visible.
所以最后的A隐藏了之前的那些.那是你问的吗?
问题是:该计划是否良好?显然这些只是片段,并没有提供完整的程序,但如果这些是A的唯一参考,那么它是否合法?我的回答是肯定的. S7.2 / 6说:
An enumeration whose underlying type is fixed is an incomplete type from its point of declaration (3.3.2) to immediately after its enum-base (if any), at which point it becomes a complete type. An enumeration whose underlying type is not fixed is an incomplete type from its point of declaration to immediately after the closing } of its enum-specifier, at which point it becomes a complete type.
因此,在给出的每个示例中,A是完整类型,如图所示. 7.2 / 3表示:
An opaque-enum-declaration is either a redeclaration of an enumeration in the current scope or a declaration of a new enumeration. [ Note: An enumeration declared by an opaque-enum-declaration has fixed underlying type and is a complete type. The list of enumerators can be provided in a later redeclaration with an enumspecifier. —end note ] A scoped enumeration shall not be later redeclared as unscoped or with a different underlying type. An unscoped enumeration shall not be later redeclared as scoped and each redeclaration shall include an enum-base specifying the same underlying type as in the original declaration.
因此,只要属性和基数相同,似乎可以重复这些声明,但是一旦提供了枚举数列表,这些声明可能就不会重复.
对于前两个,似乎程序结构良好,但也许不是最后一个.