The modern versions of C++ attempt to use the bool
operator of a class when evaluating the condition in an if()
statement. Other casting operators, such as int
being use when no bool
operator exists. This is demonstrated below.
#include <iostream>
using namespace std;
class TwoInts {
public:
int a,b;
operator bool() { cout << "TwoInts to bool" << endl; return 0;}
operator int() { cout << "TwoInts to int" << endl; return 0;}
};
class SixInts {
public:
int a[6];
operator int() { cout << "SixInts to int" << endl; return 0;}
};
int main(void) {
TwoInts T;
SixInts S;
if (T) cout << "xxx" << endl;
if (S) cout << "xxx" << endl;
return 0;
}
Running this code produces no surprises:
TwoInts to bool
SixInts to int
Looking over some old C++ code, there appears to be a change that I would like to verify.
Did older versions of C++ use the int
operator of a class when evaluating the condition in an if()
statement? What versions, if any, did that?
The suggested output then would have been
TwoInts to int
SixInts to int
Some details as to why the question: Issues with converting an old big integer class may be due int
vs. bool
in an if()
. No longer have access to the old compiler, so can not test the old behavior.
[Edit]
Using the answers below and some more research:
Answer: Yes, many pre ISO Standard C++ versions (mid 1980s -1998), that lacked a bool
, did use casts to int
(or other numeric types). Significant compiler variation existed - it was pre-standard.
The first C++ ISO standard came out in 1998 (ISO/IEC 14882:1998 aka C++98). It defined the bool
type. Thus ISO standard C++ has always used the bool
cast in if()
.
I can't directly answer the question for certain, but Herb Sutter says that there was no bool
type in the pre-standard version of C++ published in 1990.
As such, if
cannot possibly have used operator bool()
and it seems to me likely that it would have used operator int()
if it worked at all. I don't have a copy of the 1990 C++ reference manual to confirm, though.
Not in standard C++. Strictly speaking, in standard C++, the if statement attempts to convert the expression contained therein to bool
, and then changes behavior based on that.
ISO/IEC 14882:2003 4.12 [conv.bool]/1:
An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.
6.4.1 [stmt.if]/1:
If the condition (6.4) yields true the first substatement is executed. If the else part of the selection statement is present and the condition yields false, the second substatement is executed. In the second form of if statement (the one including else), if the first substatement is also an if statement then that inner if statement shall contain an else part.76)
Note the explicit reference to "true" and "false", which are boolean concepts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With