I have my enum like this.

enum Size{PAGE_SIZE=20,

I only use them as constants(No enum variable created) in my program.
i.e= int x = PAGE_SIZE + 20;

So what is the better way to declare the constants in this scenario? (enum or const)

  • 7
    It doesn't seem like Page Size and Header Size are really two members of something called size but rather two sizes. Typically I think of the different enums as being more like logical states, whereas I think you're using them as constants and happen to be storing them as enums. Am I understanding your usage correctly?
    – J Trana
    Dec 25, 2013 at 7:14
  • You could have a nameless enum
    – James
    Dec 25, 2013 at 10:44
  • So nameless enum can be used like constants where there is no need of creating enum variable?
    – sk patra
    Dec 27, 2013 at 13:18

1 Answer 1


Perhaps due to my background in other languages where the typing of enum is more strict (*), I tend to think that if you want a type able to contain a set of constants, use an enum, if you want a bunch of constants, use a bunch of constants and don't introduce a type where there is no need of one.

There have been considerations which made the choice of an enum better at the time, but the only one I can think which survived the normalization of C++ 98 is compatibility with C and previous implementations of C++ (and some have kept as their style what was a necessity when they established it).

What has survived are the considerations which made the use of an additional type worrisome. The fact that the constants are of another type which is normally not used can interfere with overload resolution and type deduction in templates. Rules are such that it does usually what you want -- perhaps at the cost of an additional instantiation for the spurious enum type -- but there are time when it isn't. For instance

template <typename T> MyClass<T> make_MyClass(T) {...}

return a MyClass<Size> for make_MyClass(PAGE_SIZE) where you probably expect a MyClass<int> if the reason for introducing the enum wasn't to have a type but introduce some constants.

(*) note that such stricter typing of enum was deemed sufficiently desirable that a variation is now provided as enum class since C++11.

  • Yes C++ enum class is more stricter type.But in my above example I am not using any Type of enum,but using like constants(Implicit conversion of enum to Int).So what is commanded in above case?Two individual constants or enum like aove.
    – sk patra
    Dec 27, 2013 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.