Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c    |    Meh, in C you gotta define EVERYTHING    |    243,242 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 241,447 of 243,242    |
|    David Brown to tTh    |
|    Re: bugprone-switch-missing-default-case    |
|    24 Oct 25 09:22:28    |
   
   From: david.brown@hesbynett.no   
      
   On 23/10/2025 23:57, tTh wrote:   
   > On 10/23/25 22:40, David Brown wrote:   
   >> Personally, I usually prefer to have the "last", "max", or "count"   
   >> indicator outside the enumeration :   
   >>   
   >> enum E1 { A, B, C, D };   
   >> static const int count_of_E1 = D;   
   >   
   > Why did you use 'static' here ?   
   >   
      
   The enumeration definition will either be in a C file, in which case the   
   const should be "static" because it is local to the file, or it will be   
   in a header, in which case making the small const "static" can be vastly   
   more efficient in many ways. I have been assuming that this definition   
   is at file scope, as most type declarations are, rather than local to a   
   function. If it is local to a function, then decent compilers will give   
   the same resulting code with or without the static.   
      
      
   But I see I have made a mistake here - I have mixed up with C++. You   
   can't use static_assert in C with a "const" like this for compile-time   
   checking. So in C, prior to C23, you actually need to write (with the   
   +1 correction to my original example) :   
      
    enum E1 { A, B, C, D };   
    enum { count_of_E1 = D + 1 };   
      
   if you want to be able to write :   
      
    _Static_assert(count_of_E1 == 4, "Checking enumeration");   
      
   With C23, you could have "constexpr int count_of_E1 = D;" and use it in   
   a static assertion. (constexpr declarations always have internal   
   linkage, so no "static" is needed.)   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca