+ 4

Why The Use Of an operand of type bool is forbidden to the 'operator++' in C++.

I Mean Why increment and decrement operator does not works on Bool values although they work fine when written as x+1 or x-1. Might be a silly question but I'm just curious bout it.thnks

30th Mar 2018, 12:19 AM
™TheChamp921
™TheChamp921 - avatar
7 Respuestas
+ 5
When we do x+1 or x-1 where x is Boolean, implicit widening conversion occurs and the result of the evaluation would be an integer value instead of a boolean. If x++ is allowed to be executed on a boolean, the program would attempt to increment the value, and then coerce it to boolean. The latter result wouldn't be what you want. (As has been explained by Satania) A simpler explanation would be that operator++ simply isn't overloaded for boolean data type.
30th Mar 2018, 1:10 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
[meta] Tracking the deprecation (where the deprecated behavior is described as "bool++ sets the boolean to true"); I did not find the original reason it was deprecated (from 1998), but the final action reason (actually finishing the deprecation) seems to be that they: * ... started in C++98 (decrement never worked anyway; found notes that it wasn't definable) * ... added std::exchange for a rare use case to help bool++ get deprecated (C++14) * ... waited nearly 20 years and finally just wanted to finish (C++17) Action mention in 1998 C++ Standard, but the paper is hard to find (withdrawn): http://www.codingstandard.com/rule/1-3-1-do-not-use-the-increment-operator-on-a-variable-of-type-bool/ https://www.iso.org/standard/25845.html Library Working Group (LWG) explanation of C++17 plans: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0002r1.html Links at the bottom of that page: "Core Issue 1653" (big page! -- short message from Bjarne Stroustrup 2013-Apr that bool++ was already deprecated "N3668 exchange() utility function", included to address a rare use case for bool++
30th Mar 2018, 1:46 AM
Kirk Schafer
Kirk Schafer - avatar
+ 5
Due to StackOverflow discussion that booleans could overflow, I explored the documentation discrepancy saying ++ just sets booleans true. Related copy+paste code for below c++17 compiler: https://code.sololearn.com/c4Nyt8o40DgP/?ref=app Output at http://cpp.sh and c++14: Max value if bool were incrementing: 255 boolean: false (0) (initial value) Boolean data: 0 (union before increment) Boolean data: 1 (increment 100) Boolean data: 1 (increment 200) loop stopped: 256 (should overflow to 0) boolean: true (1) (still set to 1)
30th Mar 2018, 2:27 AM
Kirk Schafer
Kirk Schafer - avatar
+ 5
Hatsy Rei true is 1, so I think...only non-zeroes if converted to the bool type first. Hopefully making sense, I mean in comparisons: (42 == true) // 0 by equality (42 ? "true" : "false") // "true" by condition, converted to bool https://softwareengineering.stackexchange.com/a/136933 "C: any scalar can be used in the boolean context..." "In C and C++, equality comparisons to true and false are dangerous..."
30th Mar 2018, 3:35 AM
Kirk Schafer
Kirk Schafer - avatar
+ 4
Kirk Schafer Perhaps it concerns the idea that any non-zero integer is evaluated to true? Somehow, I forgotten to consider that part.
30th Mar 2018, 3:05 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Thanks Guys For Helping Me Out. Always Smile😊#The-champ-921
30th Mar 2018, 7:02 AM
™TheChamp921
™TheChamp921 - avatar
0
thank you agent
30th Apr 2018, 7:18 PM
Kolawole Falase
Kolawole Falase - avatar