P1975R0 tweaks the static_cast wording: it says that "An expression e can be
explicitly converted to a type T if [...] T is an aggregate type having a first
element x and there is an implicit conversion sequence from e to the type of
x."  This already works for classes, e.g.:

  struct Aggr { int x; int y; };
  Aggr a = static_cast<Aggr>(1);

albeit I noticed a -Wmissing-field-initializer warning which is unlikely to be
helpful in this context, as there's nothing like static_cast<Aggr>(1, 2)
to quash that warning.

However, the proposal also mentions "If T is ``array of unknown bound of U'',
this direct-initialization defines the type of the expression as U[1]" which
suggest that this should work for arrays (they're aggregates too, after all).
Ville, can you confirm that these

  int (&&r)[3] = static_cast<int[3]>(42);
  int (&&r2)[1] = static_cast<int[]>(42);

are supposed to work now?  There's no {} variant to check.  Thanks.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

gcc/cp/ChangeLog:

        PR c++/92812
        * typeck.c (build_static_cast_1): Add warning_sentinel for
        -Wmissing-field-initializer.

gcc/testsuite/ChangeLog:

        PR c++/92812
        * g++.dg/cpp2a/paren-init27.C: New test.
---
 gcc/cp/typeck.c                           |  3 +++
 gcc/testsuite/g++.dg/cpp2a/paren-init27.C | 24 +++++++++++++++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/paren-init27.C

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 589e014f855..062751a6379 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -7472,6 +7472,9 @@ build_static_cast_1 (location_t loc, tree type, tree 
expr, bool c_cast_p,
      static_cast of the form static_cast<T>(e) if the declaration T
      t(e);" is well-formed, for some invented temporary variable
      t.  */
+
+  /* In C++20, we don't want to warn about static_cast<Aggr>(1).  */
+  warning_sentinel w (warn_missing_field_initializers);
   result = perform_direct_initialization_if_possible (type, expr,
                                                      c_cast_p, complain);
   if (result)
diff --git a/gcc/testsuite/g++.dg/cpp2a/paren-init27.C 
b/gcc/testsuite/g++.dg/cpp2a/paren-init27.C
new file mode 100644
index 00000000000..a856c7fd7be
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/paren-init27.C
@@ -0,0 +1,24 @@
+// PR c++/92812
+// P1975R0
+// { dg-do run { target c++20 } }
+// { dg-options "-Wall -Wextra" }
+
+struct Aggr { int x; int y; };
+struct Base { int i; Base(int i_) : i{i_} { } };
+struct BaseAggr : Base { };
+struct X { };
+struct AggrSDM { static X x; int i; int j; };
+
+int
+main ()
+{
+  Aggr a = static_cast<Aggr>(42);
+  if (a.x != 42 || a.y != 0)
+    __builtin_abort ();
+  BaseAggr b = static_cast<BaseAggr>(42);
+  if (b.i != 42)
+    __builtin_abort ();
+  AggrSDM s = static_cast<AggrSDM>(42);
+  if (s.i != 42 || s.j != 0)
+    __builtin_abort ();
+}

base-commit: 932fbc868ad429167a3d4d5625aa9d6dc0b4506b
-- 
2.26.2

Reply via email to