Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++14, in which scope are unscoped enumerators of redeclared enumerations declared?

Tags:

The C++14 (precisely, N4296) says with respect to enumerations, in 7.2:11:

Each enum-name and each unscoped enumerator is declared in the scope that immediately contains the enum-specifier.

Now what happens if a namespace N contains an opaque-enum-declaration of an enum E, and later the enumeration is fully declared from the global namespace? Shall we find its enumerators in the global namespace, or in the namespace N?

Of course, in order to opaque-declare an unscoped enumeration, it shall have a fixed underlying type. Consider the following piece of code.

namespace N { enum E : int; } enum N::E : int {A,B};  namespace N {     int foo() {         return int(::N::B);     } }  int bar() {     //return int(::A);     return int(A); } 

The first line in bar is commented out, because clang++ -std=c++14 says:

no member named 'A' in the global namespace; did you mean simply 'A'?

Gcc fails to compile both lines in bar(). So both gcc and clang declare the enumerations in the namespace N.

So my questions are:

  1. What is the scope that immediatelly contains the enum specifier? (I thing it is the scope of the global namespace).
  2. Should the enumerators A, B be defined in the global namespace?
  3. In the bar function, why ::A does not refer to the enumerator, but simple A does?
  4. Why the expression ::N::B in the function N::foo denotes the enumerator?

EDIT 1: the original declaration was enum ::N::E : int {A,B};, but gcc was not able to parse it (bug report), so I removed the leading colons to use enum N::E : int {A,B};

EDIT 2: the clang's behavior is a bug

like image 912
Jan Tušil Avatar asked Nov 19 '17 23:11

Jan Tušil


People also ask

What are enumeration variables How are they declared?

An enumeration type declaration gives the name of the (optional) enumeration tag. And, it defines the set of named integer identifiers (called the enumeration set, enumerator constants, enumerators, or members). A variable of the enumeration type stores one of the values of the enumeration set defined by that type.

What is Unscoped enumeration?

In an unscoped enum, the scope is the surrounding scope; in a scoped enum, the scope is the enum-list itself. In a scoped enum, the list may be empty, which in effect defines a new integral type. By using this keyword in the declaration, you specify the enum is scoped, and an identifier must be provided.

What is an enumeration in C++?

In C++ programming, enum or enumeration is a data type consisting of named values like elements, members, etc., that represent integral constants. It provides a way to define and group integral constants. It also makes the code easy to maintain and less complex.

What is enum class in C++?

Enum Class C++11 has introduced enum classes (also called scoped enumerations), that makes enumerations both strongly typed and strongly scoped. Class enum doesn't allow implicit conversion to int, and also doesn't compare enumerators from different enumerations.


1 Answers

The enumeration E is declared within the namespace N, even though its definition is set within the global namespace. As such, it can only be accessed in the scope of N.

The bar function should then be defined as:

int bar() {     return int(N::A);     //SAME AS --> return int(::N::A); } 
like image 173
Ashley Prazeres Avatar answered Oct 06 '22 00:10

Ashley Prazeres