Topic: Improvements to C++: boolean type


Author: "Peter Lupton" <pl@nch.com.au>
Date: 2 Oct 2005 04:00:02 GMT
Raw View
C++ would be a better language if the boolean type did not implicitly
convert from int. For example, many novice programmers make the
mistake.

   if (i = j) dosomething(); // Should be i == j

If conversion to boolean required explicit this would all be solved. It
would mean all the old code with expressions like "while (p) ... "
would need to be changed to "while (p != NULL) ...". But I think the
change would be well justified.

Note: C# does this.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jackklein@spamcop.net (Jack Klein)
Date: Sun, 2 Oct 2005 16:07:22 GMT
Raw View
On 2 Oct 2005 04:00:02 GMT, "Peter Lupton" <pl@nch.com.au> wrote in
comp.std.c++:

> C++ would be a better language if the boolean type did not implicitly
> convert from int. For example, many novice programmers make the
> mistake.
>
>    if (i = j) dosomething(); // Should be i == j
>
> If conversion to boolean required explicit this would all be solved. It
> would mean all the old code with expressions like "while (p) ... "
> would need to be changed to "while (p != NULL) ...". But I think the
> change would be well justified.
>
> Note: C# does this.

Note that "while(p)" has nothing at all to do with integers.

This might or might not be worthwhile, as generally there are compiler
options and other tools, such as lint, that can warn about this.

Regardless, it will never happen.  It would break far too much
existing code.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: invalid@e.mail (Kyle)
Date: Sun, 2 Oct 2005 16:08:00 GMT
Raw View
Peter Lupton wrote:
> C++ would be a better language if the boolean type did not implicitly
> convert from int. For example, many novice programmers make the
> mistake.
>
>    if (i = j) dosomething(); // Should be i == j
>
> If conversion to boolean required explicit this would all be solved. It
> would mean all the old code with expressions like "while (p) ... "
> would need to be changed to "while (p != NULL) ...". But I think the

as you stated, probably a lot of existing code would be broken by such
change and the only benefit will be that novices who dont understand
conversion rules wont be able to make some mistakes.

(moreover in case of if(i=j)... a warning is issued in at last 1
compiler i know of - no need for language change, just good will of
compiler writers)

> change would be well justified.

thats not good enough justification for a language change (IMHO),
imagine what c++ would be like if it followed the path of changing a
rule when an amount of newbies found something difficult in it

>
> Note: C# does this.
that doesnt change anything
there is a lot of MS sample code (dated but still) that uses 'void main'
does it mean we shoud have it in c++ ? no.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: chris jefferson <caj@cs.york.ac.uk>
Date: Sun, 2 Oct 2005 11:09:59 CST
Raw View
Peter Lupton wrote:
> C++ would be a better language if the boolean type did not implicitly
> convert from int. For example, many novice programmers make the
> mistake.
>
>    if (i = j) dosomething(); // Should be i == j
>
> If conversion to boolean required explicit this would all be solved. It
> would mean all the old code with expressions like "while (p) ... "
> would need to be changed to "while (p != NULL) ...". But I think the
> change would be well justified.
>

I don't think you can even begin to imagine to amount of code which this
would break. I can also tell you that many programmers would rebel at
the idea of forcing them to put != NULL.

Another common idiom, something like if(p = get_ptr()) would also be
broken. There are alot of those around as well.

I'm not saying it's a bad idea, but you are not going to get it changed!

Every compiler I've ever used will warn about this at even low levels of
warning, usually you get around it by putting an extra set of brackets
when you want them. I would say all beginner programmers (and in my
opinion all programmers) should compile code at a reasonable level of
warning (although in many compilers the max level can be excessive), and
make sure they understand all the warnings being produced and remove
them if at all possible.

Chris

> Note: C# does this.
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]
>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@comAcast.net ("Victor Bazarov")
Date: Sun, 2 Oct 2005 16:10:01 GMT
Raw View
Peter Lupton wrote:
> C++ would be a better language if the boolean type did not implicitly
> convert from int.

No, it wouldn't.  Implicit conversions are the cornerstone of the
language.

> For example, many novice programmers make the
> mistake.
>
>   if (i = j) dosomething(); // Should be i == j

Many compilers have a warning for that.

Language design should not have novice programmers as the main
target.  Novice programmers need to learn the language.  As I recall,
*I* made that mistake only once in my career.

> [..]

V


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: alfps@start.no (Alf P. Steinbach)
Date: Sun, 2 Oct 2005 20:22:19 GMT
Raw View
* Peter Lupton:
> C++ would be a better language if the boolean type did not implicitly
> convert from int.

That's a partial definition of what "better" means to you.  Anyone disagreeing
with your statement must therefore be telepaths, and/or employing flawed
reasoning.  However, your definition of "better" works for me, too.


> For example, many novice programmers make the
> mistake.
>
>    if (i = j) dosomething(); // Should be i == j
>
> If conversion to boolean required explicit this would all be solved. It
> would mean all the old code with expressions like "while (p) ... "
> would need to be changed to "while (p != NULL) ...". But I think the
> change would be well justified.
>
> Note: C# does this.

As others have noted it would break too much code, and furthermore, I suspect
that most C++ programmers do _not_ share our common idea of "better".

But that doesn't mean that something can't be done about the problem, just
that the particular solution of changing 'bool' behavior is unacceptable.

What one can do include (1) judicious use of 'const', (2) using high warning
levels, and (3) attention to detail.

Which suggests that there are ways without changing the standard; however,
about actually changing the standard, I think that


  * In addition to the existing alternative tokens 'and_eq', 'or_eq', etc.,
    which include 'not_eq' for "!=", there should be an alternative token for
    '=='.

I don't understand why that most important operator was left out.

Here's one additional technical suggestion, not for standardization but for
handling the problem of inadvertent assignment:

   if_( i = j ) dosomething();    // Compiler or linker error.

where

   #define if_( x ) if( guaranteedBool( x ) )
   #define while_( x ) while( guaranteedBool( x ) )
   #define choice_( x, r1, r2 ) (guaranteedBool( x )? r1 : r2)

and 'guaranteedBool' is a template function, like  --  off the cuff  --

   template< typename T >
   T guaranteedBool( T v ) { STATIC_ASSERT( "not bool" & false );  return v; }

   template<> bool guaranteedBool<bool>( bool v ){ return v; }

Again, that's not a suggestion for standardization! ;-)  Also, it doesn't work
for bool to bool assignment, and it may yield hopeless error messages (perhaps
even delayed to link time), and I don't quite see how to do it reasonably for
'for', and worst of all it relies on the programmer's attention to detail in
order to report errors due to lack of attention to detail...  And no, I don't
use it myself, it's just a suggestion: it would be interesting to know how it
fared if, say, a large group of students were encouraged to use these
constructions, provided by some header, with a similar control group of
students doing the same exercises etc. with ordinary C++.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "kanze" <kanze@gabi-soft.fr>
Date: Mon, 3 Oct 2005 22:54:49 CST
Raw View
Peter Lupton wrote:

> C++ would be a better language if the boolean type did not
> implicitly convert from int.

To or from int, double, pointer...

C++ would be a better language if there were no lossy
conversions, either.

In practice, either change would break so much existing code
that I just can't see it happening.

> For example, many novice
> programmers make the mistake.

>    if (i = j) dosomething(); // Should be i == j

> If conversion to boolean required explicit this would all be
> solved. It would mean all the old code with expressions like
> "while (p) ... " would need to be changed to "while (p !=
> NULL) ...". But I think the change would be well justified.

Are you the one who is going to visit all the user sites, and
change their existing code?

> Note: C# does this.

Note: C# doesn't have a past history, and was never compatible
with C.

--
James Kanze                                           GABI Software
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]