c++ - More than one enum declaration -
there quote sec. 3.3.1/4 of n3797 working draft:
given set of declarations in single declarative region, each of specifies same unqualified name,
— 1 declaration shall declare class name or enumeration name not typedef name , other declarations shall refer same variable or enumerator, or refer functions , function templates;
we can declare enum
type name several times in single declarative region:
enum a; // declared first time, other declaration same // unqualified name shall refer same variable or enumeration. enum a; // , other declarations shall refer enumeration extern int a; // fail, enum hidden , can access // via elaborated-type-specifier
i'm confused. can explain behavior? find corresponding references current working draft.
the full quote n3797 3.3.1/4 is:
given set of declarations in single declarative region, each of specifies same unqualified name,
— shall refer same entity, or refer functions , function templates; or
— 1 declaration shall declare class name or enumeration name not typedef name , other declarations shall refer same variable or enumerator, or refer functions , function templates; in case class name or enumeration name hidden.
the code wrote wrong. see s7.2 required syntax, may of these:
enum : int; // declaration (and enumeration name) enum : int; // refers same entity enum struct a; // declaration (and enumeration name) enum struct a; // refers same entity enum {}; // declaration (and enumeration name) enum {}; // refers same entity
then following may occur later in translation unit.
extern int a; // declaration hides enumeration name void f() { j = a.a; // illegal. hidden int k = a; // legal }
see n3797 3.3.10/2:
a class name (9.1) or enumeration name (7.2) can hidden name of variable, data member, function, or enumerator declared in same scope. if class or enumeration name , variable, data member, function, or enumerator declared in same scope (in order) same name, class or enumeration name hidden wherever variable, data member, function, or enumerator name visible.
so last hides earlier ones. asking?
re question: program well-formed? these fragments, , no complete program has been presented, legal if these references a
? answer yes. s7.2/6 says:
an enumeration underlying type fixed incomplete type point of declaration (3.3.2) after enum-base (if any), @ point becomes complete type. enumeration underlying type not fixed incomplete type point of declaration after closing } of enum-specifier, @ point becomes complete type.
so in each of examples given complete type shown. 7.2/3 says:
an opaque-enum-declaration either redeclaration of enumeration in current scope or declaration of new enumeration. [ note: enumeration declared opaque-enum-declaration has fixed underlying type , complete type. list of enumerators can provided in later redeclaration enumspecifier. —end note ] scoped enumeration shall not later redeclared unscoped or different underlying type. unscoped enumeration shall not later redeclared scoped , each redeclaration shall include enum-base specifying same underlying type in original declaration.
so seems these declarations can repeated long attributes , base same, perhaps not once list of enumerators has been provided.
for first 2 seem program well-formed, perhaps not last.
Comments
Post a Comment