I’m having a hard time understanding this piece of code.
#define TABLE \
STATE(STATE_1, true) \
STATE(STATE_2, false) \
STATE(STATE_3, true)
enum State_t
{
#define STATE( state, valid) state,
TABLE
#undef STATE
NUM_STATES
}
I know that State_t enum will have STATE_1, STATE_2 and STATE_3 and NUM_STATES=3, but I’m having a hard time understanding the mechanism of this. Could someone please kindly explain.
Also, now I want to define a duplicate state, for example:
#define TABLE \
STATE(STATE_1, true) \
STATE(STATE_2, false) \
STATE(STATE_3, true) \
STATE(STATE_2, true)
But this gives compiler errors because of redefinition of STATE_2. How can I make State_t enum still have State_t={STATE_1, STATE_2 , STATE_3} and if possible NUM_STATES=4, maybe using #ifndef. I’m not sure if this can be done but please share your ideas.
Thank you.
Some more answers to the following questions:
A.
The
#defineand#undefare “saying” where the define is valid. meaning, if you write “STATE(STATE_2, true)” after the#undefthe PP will not replace it.B.
the comma next to the state is what separate the enum entities.
C.
enums, unless specifically define otherwise, are giving the first entity the value 0 and increasing it by one for each value:
The name of the entity is irrelevant for the value.
I think you are lacking the basics of C++ and you trying to understand something more complicated.
try to look for a simple enum and #define examples.