Xref: utzoo comp.lang.c++:13756 comp.std.c++:945 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!uupsi!sunic!lth.se!newsuser From: dag@control.lth.se (Dag Bruck) Newsgroups: comp.lang.c++,comp.std.c++ Subject: Re: Boolean confusion Summary: How you could introduce `boolean' into the language Message-ID: <1991May30.060200.6590@lth.se> Date: 30 May 91 06:02:00 GMT References: <332@nazgul.UUCP> <1991May29.145100.18519@watson.ibm.com> <1991May29.171209.14760@neon.Stanford.EDU> Sender: newsuser@lth.se (LTH network news server) Distribution: comp.lang.c++ Organization: Department of Automatic Control, Lund, Sweden Lines: 57 In article <1991May29.171209.14760@neon.Stanford.EDU> philip@pescadero.stanford.edu writes: >.... But how many programs would break? I for one would be prepared >to pay the price for the long-term gain, but I generally try to program >as if boolean and int are distinct types, so the price wouldn't be >very high for me. The "break old code" argument against any changes is the one you hear most often at the C++ standardization meetings. It is of course valid, but often over-used. Even if there were incompatible changes, every compiler would have a switch to get the old behaviour. One of the strongest arguments in favour of a new boolean data type is that you could do function overloading: void f(int); void f(boolean); I also think a boolean data type would increase type safety in C++, at least the way I program (:-). I once tried to implement `class boolean' and failed. Firstly there was a performance problem, which I think good compilers could overcome. Secondly, I don't think I gained anything in type safety and little in convenience: to be at all useful I needed implicit type conversions int->boolean and boolean->int. I think a boolean data type should _not_ be another kind of integer (at least eventually) which can be implicitly converted to/from int. It should instead be a unique enumerated type, but that doesn't work until all standard functions (e.g., operator == (int, int)) return a boolean and not an int. The transition to a better world (:-) should be done in two phases: 1. Introduce the new built-in data type `boolean' (or whatever you call it), and specify that all standard operator == (etc.) return a boolean. 2. Include implicit type conversions between boolean and int, to minimize the number of broken programs. I think this automatic conversion will help in most cases. 3. Warn about the automatic conversions and label them as anachronisms. 4. (Some years later) Turn the warnings of (3) into errors. We had a similar case with C++ 2.0 with respect to handling of `enum', and I think the transition was relatively painless. The most common problem was that people had used an enum for booleans, which caused warning messages under the new interpretation. Dag Br!"uck -- Department of Automatic Control E-mail: dag@control.lth.se Lund Institute of Technology P. O. Box 118 Phone: +46 46-104287 S-221 00 Lund, SWEDEN Fax: +46 46-138118