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,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    
   int main () {   
       bool foo = false;   
       std::cout << "\n" << foo;   
       foo++;   
       std::cout << "\n" << foo;   
       foo++;   
       std::cout << "\n" << foo;   
   }   
      
   prints   
   0   
   1   
   1   
      
   The only use case for operator++ on bool I could think of was the following:   
      
   #include    
   #include    
      
   void print(const std::vector& vec) {   
       bool needComma = false;   
       for (auto it = vec.begin(); it != vec.end(); ++it) {   
           if (needComma++)   
               std::cout << ", ";   
           std::cout << *it;   
       }   
   }   
      
   int main () {   
       std::vector v;   
       v.push_back(1);   
       v.push_back(2);   
       v.push_back(3);   
       print(v);   
   }   
      
   Admittedly, this is a bit far-fetched will probably yield wrong results   
   for large vectors if bool is implemented using 8bit values.   
      
   Regards,   
   Stuart   
      
      
   --   
         [ 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