new-expression size check and constant evaluation
7.7 [expr.const]
Tam S. B

Created on 2018-12-05.00:00:00 last changed 5 days ago


Date: 2022-09-24.20:24:20

Proposed resolution (approved by CWG 2022-09-23):

Change in [expr.new] paragraph 8 as follows:

If the expression is erroneous after converting to std::size_t:
  • if the expression is a potentially-evaluated core constant expression, the program is ill-formed;
  • otherwise, an allocation function is not called; instead...
Date: 2022-09-24.20:24:20

According to [expr.new] paragraph 8, if the expression in a noptr-new-declarator is a core constant expression, the program is ill-formed if the expression is erroneous, e.g., negative. However, consider the following example:

  template<class T = void> constexpr int f() { T t; return 1; }
  using _ = decltype(new int[f()]);

f() is a core constant expression, so it must be evaluated to determine its value. However, because the expression appears in an unevaluated operand, it is not “potentially constant evaluated” and thus f is not “needed for constant evaluation”, so the template is not instantiated (13.9.2 [temp.inst] paragraph 7). There is implementation divergence on the handling of this example.

CWG telecon 2022-09-09:

The example should be well-formed, because f is not instantiated.

A similar situation arises for narrowing conversions, except that in the latter case, determining the value at compile-time empowers to allow additional cases, whereas the new-expression case uses a compile-time value to prohibit additional cases.

Date User Action Args
2022-09-24 20:24:20adminsetmessages: + msg6945
2022-09-24 20:24:20adminsetstatus: drafting -> ready
2022-09-09 20:27:40adminsetstatus: open -> drafting
2018-12-05 00:00:00admincreate