Title
Use of bool constants in integral constant expressions
Status
nad
Section
7.7 [expr.const]
Submitter
Andy Koenig

Created on 1999-02-18.00:00:00 last changed 299 months ago

Messages

Date: 1999-04-15.00:00:00

Rationale (04/99): A careful reading of 7.7 [expr.const] indicates that all types of literals can appear in integral constant expressions, but floating-point literals must immediately be cast to an integral type.

Date: 2022-02-18.07:47:23

Consider:

    int* p = false;         // Well-formed?
    int* q = !1;            // What about this?
>From 6.8.2 [basic.fundamental] paragraph 6: "As described below, bool values behave as integral types."

From 7.3.12 [conv.ptr] paragraph 1: "A null pointer constant is an integral constant expression rvalue of integer type that evaluates to zero."

From 7.7 [expr.const] paragraph 1: "An integral constant-expression can involve only literals, enumerators, const variables or static members of integral or enumeration types initialized with constant expressions, ..."

In 5.13.2 [lex.icon] : No mention of true or false as an integer literal.

From 5.13.7 [lex.bool] : true and false are Boolean literals.

So the definition of q is certainly valid, but the validity of p depends on how the sentence in 7.7 [expr.const] is parsed. Does it mean

    {literals, enumerators, const variables, or static members} of integral of integral or enumeration types...
Or does it mean
    literals (of any type), enumerators, const variables, or {static members of integral or enumeration types}
Or something else?

If the latter, then (3.0 < 4.0) is a constant expression, which I don't think we ever wanted. If the former, though, we have the anomalous notion that true and false are not constant expressions.

Now, you may argue that you shouldn't be allowed to convert false to a pointer. But what about this?

    static const bool debugging = false;

    // ...

    int table[debugging? n+1: n];
Whether the definition of table is well-formed hinges on whether false is an integral constant expression.

I think that it should be, and that failure to make it so was just an oversight.

Rationale (04/99): A careful reading of 7.7 [expr.const] indicates that all types of literals can appear in integral constant expressions, but floating-point literals must immediately be cast to an integral type.

History
Date User Action Args
1999-09-14 00:00:00adminsetmessages: + msg194
1999-02-18 00:00:00admincreate