zapshe wrote: |
---|
When bool = 1, it's == to true. You can use std::bitset<8>(true).to_string() and you'd get 00000001. "true" is equal to 1. |
bool b = 1;
?zapshe wrote: |
---|
Why would true be stored as 10001000.. |
zapshe wrote: |
---|
.. and why would my example output 00000001 if such is the case? |
zapshe wrote: |
---|
As far as I know, true evaluates to 1. |
1
(an int).mbozzi wrote: |
---|
I think that program has undefined behaviour, although I'm not entirely sure. |
You mean something like bool b = 1; ? |
Because the bool value true is converted to the integer value 1 when you pass it to the bitset constructor. |
|
|
00000101 |
std::bitset
and std::vector<bool>
store an array of bits, where each bit is a separate boolean value — they use some bitmasking to transform the indexed bit to and from a bool
. Some databases use all-bits set as true. Etc etc.bool
values. Code that looks like 0 < 1
is valid only because of integer type promotion. I can’t really think of a good way or reason to break that, but I also have never needed to use it.Duthomhas wrote: |
---|
It is the same reason that nullptr is not necessarily an actual zero in memory — weird old dinosaurs exist where it isn’t. |
|
|
Either way, anything that's not 0 is considered true. |
|
|
For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true. |
The C++20 standard (6.8.210) states: Type bool is a distinct type that has the same object representation, value representation, and alignment requirements as an implementation-defined unsigned integer type. The values of type bool are true and false. However, if the bool type can have just two values, it cannot have the same value representation as the underlying unsigned integer type, which can take at least 256 distinct values. In view of this contradiction, no implementation can conform exactly to the standard; they approximate the standard in different ways, with observably different consequences. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2624r0.html |
6.8.2 Fundamental types [basic.fundamental] 10 Type bool is a distinct type that has the same object representation, |
(i) A minimal resolution of the issue would be to have the standard state that undefined behavior results whenever the unsigned integer object holding a bool has a value other than 0 or 1. This option would obviate the need to amend any existing implementation but, to be fully portable, code would need to check the value of a bool of unknown provenance before using it, such as by promoting it to int and converting it back to bool. (ii) A maximal resolution would be to specify a common representation of bools for all implementations. This would maximize portability but would involve greater changes to some implementations’ semantics for bools. |
Whether it is undefined behaviour or unspecified behaviour, if the programmer expects a value other than true or false as a valid answer, why would it be represented as a bool? |
my c++ knowledge is pre-cx98 |
ninja01 wrote: |
---|
I am following each comment with enthusiasm, but i cannot reply simply because my c++ knowledge is pre-cx98 and it sucks, also I was expecting the answer to be simple, but you took this topic to higher level I still not reached yet. |
ninja01 wrote: |
---|
@Peter87 just provide one of the best link I ever saw. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2624r0.html What is the story of this website? It seems to me that it is gathering of programmers finding the bugs about the standard, am I right? |
I am currently reading the 4th edition of c++ programming language |