1

I recently received some (closed source) third-party code to work with. While scrolling through all the classes (c++) I found some interesting snippet that I've never seen anyone doing before.

void ns::somefunc() {
  enum { MAX_ATTEMPS = 5 }; // why not 'const int max_attemps = 5;'?
  for (int i = 0; i < MAX_ATTEMPS; ++i) {
    /* some code without reference to MAX_ATTEMPS or i */
  }
}

I've never seen this, why would one create an unnamed enum - within a function - with a single enumerator set to a specific value instead just using const int max_attemps = 5;? Especially since neither i nor MAX_ATTEMPS are used inside the loop or anywhere else in the functions scope.

bam
  • 954
  • 8
  • 26
  • 3
    Does this answer your question? [The usage of anonymous enums](https://stackoverflow.com/questions/7147008/the-usage-of-anonymous-enums) – aep Jan 07 '20 at 05:57

0 Answers0