Home > database >  Process of conversion of types inside selection and iteration statements in C
Process of conversion of types inside selection and iteration statements in C

Time:09-28

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)

CodePudding user response:

Comments already discussed that you are misinterpreting the two paragraphs. I will focus on the second. The important message is the following:

There are certain contexts where values might implicitly undergo converions even though the conversion is actually only possible explicitly.

Some code examples might help:

struct foo{
    explicit operator bool() {return true;}
};

int main()
{
   foo f;
   bool b(f);    // fine !
   bool c = f;   // error! no viable conversion from f to bool
                 // because foo::operator bool is explicit
}

A foo can be converted to a bool but the conversion operator is explicit. Hence bool b(f) (explicit conversion) is fine, while bool c = f; (implicit conversion) is not.

Now, there are certain contexts where...

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).

This explains some special case of conversions. Such conversion happen implicitly, but are well-formed exactly when the explicit conversion would be well formed.

With out such "contextual conversion", this would be a compiler error:

 foo f;
 if (f) {}

However, because f is contextually convertible to bool, the code is ok. Without this special rule for contextual conversion one would have to write if (bool(f)) because foo::operator bool is explicit.

In other words, the paragraph is not about bools getting converted to bool, but rather it explains an exception from the usual implicit / explicit conversions, that are applied when necessary.

  • Related