vendredi 24 septembre 2021

Process of conversion of types inside selection and iteration statements in C++

According To C++ ISO:

The value of a condition that is an initialized declaration in a statement other than a switch statement is the value of the declared variable contextually converted to bool (7.3). If that conversion is ill-formed, the program is ill-formed. The value of a condition that is an initialized declaration in a switch statement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted to integral or enumeration type otherwise. The value of a condition that is an expression is the value of the expression, contextually converted to bool for statements other than switch; if that conversion is ill-formed, the program is ill-formed.

The following quote comes from the section 7.3 said above:

Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (9.4).

Based on these two, I got an idea that switch-statement not always conducts conversions if it has the appropriate type. Otherwise if-statement looks to always perform such conversion, even if I do something like if (true){},I understood the true value would be converted. So, is it what happens? The code: if(true){} will convert true to boolean?(even true already being a boolean)

Aucun commentaire:

Enregistrer un commentaire