Robert Ramey wrote:
Daryle Walker wrote:
As I said in another post, trying this on an uninitialized "bool" has already given you undefined behavior, so you don't know if you'll reach your assertion code, let alone trigger it. (Also the 0 or 1 from the conversion doesn't tell you anything from the internal representation.)
Hmmm - suppose I implement the following code:
bool tf; int i = tf; assert(0 == i || 1 == i);
and the assertion is invoked. Is it not correct to conclude that there is a problem with the code and that it should be somehow altered?
As Daryle and others have pointed out, citing the C++ standard, accessing an uninitialized bool variable gives one undefined behavior. In the face of that, why do you think that the above 3 lines of code mean anything ? Because in X compiler it may work to trigger the assert ? That is not the way to program C++ in this case. The assert above is meaningless. Whether it occurs or not is completely random. A program could pass an unitialized bool variable for serialization and if you used the code above to test for an uninitialized bool it would only prove that if some compiler set an unitialized bool variable to a value which did not have a bit pattern of 0 or 1 in that particular situation, the assert would occur. In the meantime if that same compiler, in some other situation having to do with unitialized bools, or any other compiler in any situation dealing with uninitialized bools, set the value to have a bit pattern of 0 or 1, the assert would not occur even though the bool would still be uninitialized. Knowing this, what would be the point of such code ? Quite simply there is no knowing what the bit pattern of an unitialized bool is, so the assert above is just a waste of time. If there were a way of testing for an unitialized bool in C++ I could see your doing so as a help to programmers who erroneously pass one to the serialization library, although I still think this sort of error is outside the bounds of your library, just as checking for an unitialized variable of any data type is outside the bounds of your library. But since there is no reliable way of doing this, it should not be done.