Robert Ramey wrote:
Nigel Rantor wrote:
Robert Ramey wrote: Pragmatic:
Well, how about simply treating anything other than 1 as false? I realise this means that you are implicitly initialising someone elses variable should they serialise and then deserialise but it would seem to preserve the effect that you would witness should you use such a variable without performing that set of operations anyway so it would be an "invisible" side-effect.
Correct:
Initialise all your variables. Shoot all programmers who don't!
And of course, the one true way - tell everyone to initialise their variables or bad things might happen and then be lenient on parsing anyway.
Actually, my preferred way would be to trap the usage of an unitialized bool variable when it is saved. Its not clear that I can do this. But a close substiture might be to convert the variable to an integer, throw an exception if its not equal to 0 or 1 and serialize it otherwise.
Sorry, I didn't read the later postings before responding to Robert's reply. This is probably the ideal behaviour as it traps the error as soon as it happens. Are there any other types that have restrictions on their values? I can't think of any, apart from float and double, where not all bit patterns correspond to a floating-point value. How does the serialization library handle NaN and +/-infinity? Oh, and consider me shot - I'm hunting down my uninitialised variables as we speak :-) Paul