Why is opaque recitation not a definition?

§3.1 / 2 states that the declaration of an opaque enum is a declaration that is not a definition. However, it takes up space in memory. Compare it with a class definitionwhich also has a size. Both standards are standard. Why is one a declaration and the other 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 is described below enum A : int;, as I can understand.

#include <iostream>
enum A : int;   // opaque-enum-declaration

int main() {
    A a;
    std::cout << a << '\n';
}

Edit1

As for the variable a, there is no difference between the previous fragment and the one below. They both leave the variable undefined. Thus, it is difficult to recognize what enum : int;is an announcement 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';
}
+4
3

, enum:

7.2 [dlc.enum]/3

-enum- - . [: , opaque-enum, . ( enumspecifier. -end note] A . , enum , .

, sizeof -enum-, , , . - , , enum-.

, opaque-enum [half], , , , , - -. , .

+2

- enum. , enum , enum undefined, . , enum A : int promises - ,

  • enum A ,
  • enum A , int.

enum A, , , enum . enum A .

, class B : , class B ; , .

EDIT:

enum- A: int; , .

XYZ , , XYZ . enum A undefined , main, :

#include <iostream>
enum A : int; // opaque-enum-declaration

int main() {
    A a;
    std::cout << a << '\n';
}
// This is allowed
enum A : int {quick, brown, fox};

, , ++ undefined .

+2

enum A : int;

- . , A . .

class B{};      

. , . . , .

std::cout << sizeof(A) << '\n';
std::cout << sizeof(B) << '\n';

, , .

#include <iostream>

int main()
{
   std::cout << sizeof( int ) << std::endl;
}

int. .

#include <iostream>

int main()
{
   int x;
   std::cout << sizeof( x ) << std::endl;
}

An object of type x was specified in this program. It really takes memory. int is just a type specifier. Similarly, A and B from your code are type specifiers. Type specifiers are just some of the descriptions. They do not take up memory.

0
source

Source: https://habr.com/ru/post/1538399/


All Articles