/* P1009: Array size deduction in new-expressions. */
const bool array_p = TREE_CODE (type) == ARRAY_TYPE;
- if (*init && (array_p || (nelts && cxx_dialect >= cxx20)))
+ if (*init
+ /* If ARRAY_P, we have to deduce the array bound. For C++20 paren-init,
+ we have to process the parenthesized-list. But don't do it for (),
+ which is value-initialization, and INIT should stay empty. */
+ && (array_p || (cxx_dialect >= cxx20 && nelts && !(*init)->is_empty ())))
{
/* This means we have 'new T[]()'. */
if ((*init)->is_empty ())
--- /dev/null
+// PR c++/97523
+// { dg-do compile }
+// We were turning the () into {} which made it seem like
+// aggregate-initialization (we are dealing with arrays here), which
+// performs copy-initialization, which only accepts converting constructors.
+
+struct T {
+ explicit T();
+ T(int);
+};
+
+void
+fn (int n)
+{
+ new T[1]();
+ new T[2]();
+ new T[3]();
+ new T[n]();
+#if __cpp_aggregate_paren_init
+ new T[]();
+ new T[2](1, 2);
+ // T[2] is initialized via copy-initialization, so we can't call
+ // explicit T().
+ new T[3](1, 2); // { dg-error "explicit constructor" "" { target c++20 } }
+#endif
+}
--- /dev/null
+// PR c++/97523
+// { dg-do compile { target c++11 } }
+
+// [expr.new]/24: If the new-expression creates an object or an array of
+// objects of class type, access and ambiguity control are done for the
+// [...] constructor selected for the initialization (if any).
+// NB: We only check for a default constructor if the array has a non-constant
+// bound, or there are insufficient initializers. Since an array is an
+// aggregate, we perform aggregate-initialization, which performs
+// copy-initialization, so we only accept converting constructors.
+
+struct T {
+ explicit T();
+ T(int);
+};
+
+struct S {
+ S(int);
+};
+
+void
+fn (int n)
+{
+ new T[1]{}; // { dg-error "explicit constructor" }
+ new T[2]{1, 2};
+ new T[3]{1, 2}; // { dg-error "explicit constructor" }
+ new T[n]{}; // { dg-error "explicit constructor" }
+
+ new S[1]{}; // { dg-error "could not convert" }
+ new S[2]{1, 2};
+ new S[3]{1, 2}; // { dg-error "could not convert" }
+ new S[n]{}; // { dg-error "could not convert" }
+}