Question: Number of elements in an enumeration - enum

Hello
how can we find out the number of elements in an enumeration:

enum Arduino{NANO=5, UNO= 7, MEGA=0};

number of elements = ?

I spent some time researching ths for a project a while back and found that the way to do it is by adding a final element that is the counter. But you'll need to use the automatic value assignment for that to work.

our coding guidelines at Qualcomm required us to define NULL and LAST enumerations

emum My_e {
    My_Null,
    My_A,
    My_B,
    My_Last
}

It is a surprising lack in C++.

Here's a way round it to let you iterate over an enum: https://crascit.com/2015/01/14/iterating-over-an-enum/

You could use it to count them, or more likely you wanted to loop through them anyway.

Hello,
and thanks for the numerous and constructive answers.
I will determine the number of elements using an array:
enum Arduino{NANO=8,UNO=9,MEGA=10,ESP=5};
const int Arduino={NANO,UNO,MEGA,ESP};

Just don’t throw away memory.
No need for it’s.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.