§3.1/2 says that an opaque-enum-declaration is a declaration that is not a definition. Nevertheless it occupies space in memory. Compare it with a class definition that also has a size. Both are complete types by the Standard. Why one is a declaration and the other is a definition?
#include <iostream>
enum A : int; // opaque-enum-declaration
class B{}; // a class definition
int main() {
std::cout << sizeof(A) << '\n';
std::cout << sizeof(B) << '\n';
}
Output
4
1
Edit
The opaque-enum-declaration enum A : int;
below is defined as far as I can understand.
#include <iostream>
enum A : int; // opaque-enum-declaration
int main() {
A a;
std::cout << a << '\n';
}
Edit1
As far as the variable a
is concerned, there's no difference between the prior snippet and the one below. They both leave the variable undefined. Thus, it's difficult to accept that enum : int;
is a declaration and enum A : int {quick, brown, fox};
is a definition.
#include <iostream>
enum A : int {quick, brown, fox};
int main() {
A a;
std::cout << a << '\n';
}
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.
Because enum definitions cannot reference one another, and no enum definition can cross-reference another type, the forward declaration of an enumeration type is never necessary.
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.
Enumerated Types or Enums in C++ Enumerated type (enumeration) is a user-defined data type which can be assigned some limited values. These values are defined by the programmer at the time of declaring the enumerated type. If we assign a float value in a character value, then the compiler generates an error.
The type is complete, although not defined after an opaque-enum-declaration:
7.2 [dlc.enum]/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.
The complete type above means that you can use sizeof
after just seeing the opaque-enum-declaration, which seems to be your concern. It is still not a definition, as a definition require all aspects of the type to be specified, which the opaque-enum-declaration does not.
If you were to compare this with a class definition, the opaque-enum-declaration would be the equivalent of a class [half] definition containing all non-static data members and an attribute stating whether virtual functions will be present, but without declaring any of the member functions. The size of the object would be clear, but not how to use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With