Recently, in this question I saw an enum used to define a single value. eg:
enum { BITS_PER_WORD = 32 };
Instead of:
#define BITS_PER_WORD 32
Assuming more members won't be added later, what - if any, are the advantages of doing this? (or is this more a a question of personal taste )
Said differently, if I have existing code using one-off int defines, is there any good reason to change these around for one-off enums shown above?
Out of curiosity I compared GCC's optimized assembler output for some non-trivial code and the result was unchanged betweem enums/defines.
Enumeration constants have several advantages:
Macros have several different advantages:
#if BITS_PER_WORD == 32
won't work if BITS_PER_WORD
is an enumeration constant).#undef
) when no longer needed.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