Enum type size: how to determine it?

Size of the enum type: can you answer to this C++ quiz?

Definition of the โ€œenumโ€ type:

An enumeration is a distinct type whose value is restricted to a range of values (see below for details), which may include several explicitly named constants (โ€œenumeratorsโ€œ). The values of the constants are values of an integral type known as the underlying type of the enumeration.

CppReference.com

Size of the enum type

If you have read carefully the definition of the enum type, then it should be easy for you to provide the correct answer to the following problem:

#include <iostream>
enum foo
{
    typeA = 0,
    typeB,
    typeC
};
enum bar : typename uint64_t
{
    typeD = 0,
    typeE,
    typeF
};
int main()
{
    std::cout << "Size of <foo> type variable: " << sizeof(foo) << " bytes" << std::endl;
    std::cout << "Size of <bar> type variable: " << sizeof(bar) << " bytes" << std::endl;
    return 0;
}

Which is the calculated size of the enum type?

Please, find the correct answer below:


Unlock now all the premium content in this page.

Sign-inย or register to buy access to this content using yourย virtual coins.


 

Pietro L. Carotenuto
Pietro L. Carotenuto

C++ Software Engineer, Ph.D. in Information Engineering. I like reading and learning new things while working on new projects. Main author on PietroLC.com looking for contributions and guest authors.

Articles: 26

Leave your feedback here:

Your email address will not be published. Required fields are marked *