Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

371
Views
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)

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!