[cfe-dev] discriminating explicit boolean expressions from implicit boolean expressions in the AST

Richard legalize at xmission.com
Thu Mar 26 12:04:00 PDT 2015


In article <E1YbBnb-00023F-6K at shell.xmission.com>,
    Richard <legalize at xmission.com> writes:

> I think this is where the wording in the standard allows you to write:
> 
> if (sp) { ... } else { ... }
> 
> without having to write an explicit conversion to bool.  I didn't look
> up the language of how the ternary expression interprets the
> conditional expression.  I will do that and see what it says.

As I read the standard, the same interpretation is applied to
conditions of if statements as is applied to the condition in a
ternary operator:

4. [conv] 

    "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 (8.5)."

5.16 [expr.cond]

    "The first expression is contextually converted to bool (Clause 4)."

6.4 [stmt.select]

    "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 (Clause 4)."

So I would be in step with the standard if I replaced all expressions
e with bool(e), but I'd like to avoid the unnecessary conversion where
possible.
-- 
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
     The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
         The Terminals Wiki <http://terminals.classiccmp.org>
  Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>



More information about the cfe-dev mailing list