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,629 of 33,346    |
|    Stuart to All    |
|    Re: Useful applications for boolean incr    |
|    02 Nov 12 11:31:59    |
   
   From: DerTopper@web.de   
      
   Am 10/21/12 10:52 PM, schrieb Daniel Krügler:   
   >   
   > 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.   
      
   I can only imagine one reason why someone would want to use operator++   
   on a bool variable, which would be that he wants to negate the value in   
   a single line:   
      
   bool negate (bool value) {   
    return ++value;   
   }   
      
   Such negation could be used like this:   
      
   void OnSomeCheckboxClicked () {   
    if (++checkboxState) {   
    doSomeChecks ();   
    }   
   }   
      
   instead of   
      
   void OnSomeCheckboxClicked () {   
    checkboxState = !checkboxState;   
    if (checkboxState) {   
    doSomeChecks ();   
    }   
   }   
      
   Apparently operator++ does not work like this (it seems to invoke   
   operator++ on the internal representation of a bool):   
      
   #include
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca