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

2022-07-01


2039. Constant conversions to bool

Section: 14.5  [except.spec]     Status: CD4     Submitter: Richard Smith     Date: 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:

  2. ...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.7.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.
  3. Change 9.1 [dcl.pre] paragraph 6 as follows:

  4. In a static_assert-declaration, the constant-expression shall be a contextually converted constant 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...
  5. Change 14.5 [except.spec] paragraph 1 as follows:

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