What does the C++ language definition promise about casting a char to bool then back to char again?
char original = 255;
bool next = original;
char final = next;
Also, what do most compilers do in this case beyond what the language guarantees?
What does the C++ language definition promise about casting a char to bool then back to char again?
char original = 255;
bool next = original;
char final = next;
Also, what do most compilers do in this case beyond what the language guarantees?
This will give a value of zero or one, depending on whether the original value was zero or non-zero.
Converting to
boolgives a value oftrueorfalse:Converting back to
charconvertsfalseto zero, andtrueto one: