nonptr-new-declarator 中的错误表达式

Erroneous expression in a nonptr-new-declarator

在 [expr.new]/7 中有以下短语:

If the expression, after converting to std::size_t, is a core constant expression and the expression is erroneous, the program is ill-formed. Otherwise, a new-expression with an erroneous expression does not call an allocation function and terminates by throwing an exception of a type that would match a handler (15.3) of type std::bad_array_new_length.

区分错误的核心常量表达式和错误的表达式有什么意义?

您引用的前一段表达了在什么情况下表达式是错误的。从本质上讲,它归结为值是负值还是太大的问题。如果是,则错误。

一个核心常量表达式是编译器可以并且将在编译时计算的东西。因此,如果可以静态地确定相应的表达式是错误的,则它是一个编译时错误。如果表达式不是常量,则可能无法在编译期间确定其值。但是,在那种情况下,将在 运行 时检测到错误,并且该错误必须导致异常。