CWG Issue 2039 (original) (raw)

This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 118e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-11-05


2039. Constant conversions to bool

Section: 14.5 [except.spec]Status: CD4Submitter: Richard SmithDate: 2014-11-09

[Adopted at the February, 2016 meeting.]

According to 14.5 [except.spec] paragraph 1,

In a noexcept-specification, the constant-expression, if supplied, shall be a constant expression (7.7 [expr.const]) that is contextually converted to bool ( 7.3 [conv]).

This allows the expression to have any type that can be converted to bool, which is too lenient; it should instead say something like “a converted constant expression of type bool(7.7 [expr.const]).” This would include the conversion to bool in the determination of whether the expression is constant or not and would also disallow narrowing conversions.

A similar consideration applies to static_assert(9.1 [dcl.pre] paragraph 6), which should probably be recast to something like, “an expression that is a constant expression (7.7 [expr.const]) after contextual conversion to bool (7.3 [conv]).”

Proposed resolution (September, 2015):

  1. Change 7.7 [expr.const] paragraph 4 as follows:

...binds directly. [_Note:_ such expressions may be used in new expressions (7.6.2.8 [expr.new]), as case expressions (8.5.3 [stmt.switch]), as enumerator initializers if the underlying type is fixed (9.8.1 [dcl.enum]), as array bounds (9.3.4.5 [dcl.array]), and as non-type template arguments (13.4 [temp.arg]). —_end note_] A contextually converted constant expression of type bool is an expression, contextually converted to bool (7.3 [conv]), where the converted expression is a constant expression and the conversion sequence contains only the conversions above.

  1. Change 9.1 [dcl.pre] paragraph 6 as follows:

In a static_assert-declaration,the constant-expression shall be a contextually convertedconstant expression of type bool(7.7 [expr.const]) that can be contextually converted to bool (7.3 [conv]). If the value of the expression when so converted is true...

  1. Change 14.5 [except.spec] paragraph 1 as follows:

In a noexcept-specification, the constant-expression, if supplied, shall be a contextually converted constant expressionof type bool (7.7 [expr.const]) that is contextually converted to bool ( 7.3 [conv]). A ( token that follows noexcept...