home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,602 of 33,346   
   Ivan Godard to All   
   Re: Useful applications for boolean incr   
   21 Oct 12 19:20:15   
   
   From: igodard@pacbell.net   
      
   On 10/21/2012 1:52 PM, Daniel Krügler wrote:   
   >   
   > I would like to know whether there are C++ projects or maybe use-cases   
   > out in the wild that have been found useful applications for the   
   > existing language support of pre- and post-increment on bool values,   
   > such as in:   
   >   
   > bool pre_inc(bool v) { return ++v; }   
   > bool post_inc(bool v) { return v++; }   
   >   
   > Currently, this support is deprecated and the C++ committee is   
   > considering to remove it completely.   
   >   
   > If anyone feels that this removal would break relevant code or idioms,   
   > please respond to this query. Short code examples would be very much   
   > appreciated. If that is not possible, but you can point to projects,   
   > it would be helpful to get links to such projects.   
   >   
   > Thanks && Greetings from Bremen,   
   >   
   > Daniel Krügler   
   >   
   >   
      
   I frequently use increment over an enumeration, typically when iterating   
   over an array whose index set is an enum. This construct is not native   
   to C/C++, but with type traits that give lower/upper bounds for enum   
   types and a little meta-programming you can write:   
   	enum E(f, g, h);   
   	array a, b;   
   	forEach(x, thru()) {   
   		a[x] = 17;   
   		b[x + 1] = 23;   
   		}   
   The metaprogramming ensures that a[5] is illegal. ++ and -- are defined   
   as the successor and predecessor operations in the natural way, as are E   
   +/- integral and E - E (but of course not E + E) in the obvious way.   
   That is, the set of arithmetic operations are the same as for pointers.   
      
   Now all this is to patch the monstrosity that is enum in C. The   
   relevance to your question is that, in any reasonable semantics, type   
   bool would be an enum{false, true} and not an integer. We actually   
   support use of bool instead of an enum in the above constructs, but the   
   metaprogramming to do so is painful.   
      
   Consequently I would suggest that rather than make bool even more of a   
   special case by removing ++, you should instead fix enum and make bool   
   an instance thereof.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- 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