Re[26]: Стиль написания - if vs ?:
От: Masterkent  
Дата: 22.04.10 12:45
Оценка: 8 (1)
uzhas:

U>Покурил немного стандартик

U>

U>13.3.3.1.1 Standard conversion sequences
U>фрагмент из Table 9:
U>Conversion, Rank, Subclause

U>"Integral Promotion" Promotion 4.5
U>"Boolean conversions" Conversion 4.12

U>Как из двух "implicit conversion sequences" найти наилучшую описывается здесь

13.3.3.2 Ranking implicit conversion sequences

U>

U>Standard conversion sequences are ordered by their ranks: an Exact Match is a better conversion than a Promotion,
U>which is a better conversion than a Conversion.

U>В двух словах: так как Promotion лучше, чем Conversion, то будет применяться Integral promotion и bool продвинется до int.

Курить стандартик надо с умом, а то ведь ненароком и обкуриться можно.

Если TRUE имеет тип int, то в сравнении TRUE == true какие-либо implicit conversion sequences не рассматриваются вообще.

C++03 — 13.3.3.1/1:

An implicit conversion sequence is a sequence of conversions used to convert an argument in a function call to the type of the corresponding parameter of the function being called.

Operator functions рассматриваются только если хотя бы один из операндов имеет классовый или перечислительный тип:

13.3.1.2 Operators in expressions /2:

If either operand has a type that is a class or an enumeration, a user-defined operator function might be declared that implements this operator or a user-defined conversion can be necessary to convert the operand to a type that is appropriate for a built-in operator. In this case, overload resolution is used to determine which operator function or built-in operator is to be invoked to implement the operator. Therefore, the operator notation is first transformed to the equivalent function-call notation as summarized in Table 8

В данном же случае следует руководствоваться исключительно правилами для встроенных операторов, описанными в разделе 5:

13.3.1.2/1:

If no operand of an operator in an expression has a type that is a class or an enumeration, the operator is assumed to be a built-in operator and interpreted according to clause 5.

5.10/1:

The == (equal to) and the != (not equal to) operators have the same semantic restrictions, conversions, and result type as the relational operators except for their lower precedence and truth-value result.

5.9 Relational operators /2:

The usual arithmetic conversions are performed on operands of arithmetic or enumeration type.

5/9:

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

— If either operand is of type long double, the other shall be converted to long double.
— Otherwise, if either operand is double, the other shall be converted to double.
— Otherwise, if either operand is float, the other shall be converted to float.
— Otherwise, the integral promotions (4.5) shall be performed on both operands. [Footnote: As a consequence, operands of type bool, wchar_t, or an enumerated type are converted to some integral type]
— Then, if either operand is unsigned long the other shall be converted to unsigned long.
— Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int.
— Otherwise, if either operand is long, the other shall be converted to long.
— Otherwise, if either operand is unsigned, the other shall be converted to unsigned.

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.

Re[27]: Стиль написания - if vs ?:
От: uzhas Ниоткуда  
Дата: 22.04.10 18:31
Оценка:
Здравствуйте, Masterkent, Вы писали:

M>Курить стандартик надо с умом, а то ведь ненароком и обкуриться можно.


Бобер, выдыхай! (с)
Спасибо
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.