From: david.brown@hesbynett.no   
      
   On 23/10/2025 22:51, Scott Lurndal wrote:   
   > David Brown writes:   
   >> On 23/10/2025 18:15, Thiago Adams wrote:   
   >>> On 10/23/2025 12:06 PM, David Brown wrote:   
   >   
   >> People do this regularly. 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;   
   >   
   > D has the value '3', yet there are four elements in the   
   > set. Perhaps the name should be count_of_E1_minus_1?   
   >   
      
   Argh! That's the danger of writing code directly in Usenet posts - it's   
   easy to make the silliest of mistakes. Clearly "D" is fine for "max" or   
   "last", but a "count" would want "D + 1".   
      
   > How do you use count_of_E1 in this case?   
   >   
   > Too bad 'sizeof' doesn't work on typed enums.   
      
   Sizeof does work on enumerated types :   
      
    enum E : unsigned short { a, b, c, d, e, f };   
      
    int x = sizeof(enum E);   
    int y = sizeof(a);   
      
   "x" and "y" are both 2 on x86. It would be a terrible mistake for it to   
   be anything else here.   
      
   A better possibility is the _Lengthof operator in the future C2y   
   standard. It is for arrays, giving the number of elements (rather than   
   the size). I believe it could be practical to have it apply to   
   enumeration types and give the count of the elements, though it is not   
   clear what it should do if the enumeration constants are explicitly   
   initialised in different ways.   
      
   (For those that don't obsessively read the latest standards, specifying   
   the underlying type of an enumeration is a C23 addition. But sizeof   
   works without that too.)   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|