[RESOLVED] The true bool assignment?

Hi,

I'm back here after a bit of a pause.

Got a real simple question pertaining to the exact specification of the behavior of bool in the C++ language.

What is the proper method to assign a boolean value (data type bool) to the result of a comparison?

I know that in C the result of a comparison is specified to be of type int and its value either 0 or 1. However I am a bit uncertain how this might work in C++ with the variable type bool.

I tend to favor the second form in the sample below. However, I would prefer to use the first form. Although I fear that the first form might rely on an implicit cast which, as far as I am concerned, would be a possible form of concern regarding portability.

Can anyone comment on the proper way to set a bool to the result of a comparison?

Sincerely, Chris.

static int a(void) { return 1; }
static int b(void) { return 2; }

int main(void)
{
const bool a_LT_b_type01 = ::a() < ::b();
const bool a_LT_b_type02 = static_cast<bool>(::a() < ::b());
const bool a_LT_b_type03 = ::a() < ::b() ? true : false;
}
[1175 byte] By [dude_1967] at [2007-11-20 11:47:36]
# 1 Re: [RESOLVED] The true bool assignment?
1 and 3 are both valid. 1 is simpler. No need for the cast in 2.
GCDEF at 2007-11-9 1:26:17 >
# 2 Re: [RESOLVED] The true bool assignment?
The operands shall have arithmetic, enumeration or pointer type. The operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) all yield false or true. The type of the result is bool.

The result is a bool.

The result is a bool.

HTH
Graham at 2007-11-9 1:27:14 >
# 3 Re: [RESOLVED] The true bool assignment?
Thanks to all.
That clears it up.

Sincerely, Chris.
dude_1967 at 2007-11-9 1:28:13 >