On Jul 15, 7:46 am, Neelesh Bodas <neelesh.bo...@gmail.comwrote:

Can somebody please explain the fine difference between these two

clauses of the standard:

4.5 (Integral promotions)

4. An rvalue of type bool can be converted to an rvalue of type

int, with false becoming zero and true becoming one

4.7 (Integral conversions)

4. If the source type is bool, the value false is converted to zero

and the value true is converted to one.

Integral promotions always occur in a certain number of cases,

whether they are needed or not. Integral conversions only occur

if needed. Thus:

short s1,s2 ;

long l1 ;

l1 = s1 + s2 ;

Both s1 and s2 are converted to int (integral promotion),

although there's not an int in sight, and not real reason (other

than the standard says so) for the conversion. The result (an

int) is converted to long (integral conversion) because a long

is what is needed; otherwise, it would remain an int.

Thus, is bool-to-int an integral promotion or integral conversion?

Integral promotion:

bool b1 = true, b2 = true ;

std::cout << b1 + b2 << std::endl ;

should output 2, because as an operator to an arithmetic operand

in an expression, integral promotion occurs, the addition is

done on int's, and the result is an int (not a bool!). Also

note:

std::cout << std::boolalpha << ! true << std::endl ;

std::cout << std::boolalpha << ~ true << std::endl ;

In the first, there is a logical operation, everything takes

place as a bool, and a bool is output. In the second, the ~

operator forces integral promotion, the results are int, and are

output as such.

void foo(const int&);

void bar(int);

bool b = false;

int i = false; // here?

foo(b); // here?

bar(b); //here?

These are all conversions. But a promotion is also a

conversion, and will be used as such.

Note that the distinction also affects operator overload

resolution. A promotion is a better match than other

conversions.

--

James Kanze (Gabi Software) email:

ja*********@gmail.com
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