Maybe I gave a too simple example on my first post. Another example common error which would be prevented by blocking implicit bool conversion might be:

   if (strcmp(szMyName, "Peter"))  printf("Hello Peter");

This bad error currently just slips through. If we gave an option to only accept explicit bools then the error would be caught (because strcmp returns int not bool).


At 10:27 AM 3/10/2005, Nicholas Nethercote 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.

What about a GCC option to turn off implicit conversion to boolean?

[~] more a.cpp
int main(void)
{
    int i = 0, j = 0;
    if (i = j)
        return 0;
    else
        return 1;
}
[~] g++ -Wall a.cpp
a.cpp: In function `int main()':
a.cpp:4: warning: suggest parentheses around assignment used as truth value

Nick


Peter Lupton
[EMAIL PROTECTED]

NCH Swift Sound
Unit 13, Level 3
28 University Avenue
GPO Box 1169
Canberra ACT 2601
Australia
www.nch.com.au

Reply via email to