https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108218
--- Comment #7 from Steven Sun <StevenSun2021 at hotmail dot com> --- I got one simple idea as a workaround. I do not have the resources to do the tests. I agree anyone to take the following patch or the idea. >From 35b4186a0ed3671de603bed6df5fb1156f087581 Mon Sep 17 00:00:00 2001 From: Steven Sun <stevensun2...@hotmail.com> Date: Sun, 25 Dec 2022 06:44:43 +0800 Subject: [PATCH] c++: escape unevaluated context in new-expression --- gcc/cp/init.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index b49a7ca9169..974ea95959e 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -3929,7 +3929,12 @@ build_new (location_t loc, vec<tree, va_gc> **placement, tree type, /* Try to determine the constant value only for the purposes of the diagnostic below but continue to use the original value and handle const folding later. */ + /* Escape the possible unevaluated context. Constant folding does + not work in unevaluated context, but is required in nelts. */ + int old_cp_unevaluated_operand = cp_unevaluated_operand; + cp_unevaluated_operand = false; const_tree cst_nelts = fold_non_dependent_expr (nelts, complain); + cp_unevaluated_operand = old_cp_unevaluated_operand; /* The expression in a noptr-new-declarator is erroneous if it's of non-class type and its value before converting to std::size_t is -- 2.34.1