]> gcc.gnu.org Git - gcc.git/commitdiff
c++: C++23 operator[] allows default arguments
authorJason Merrill <jason@redhat.com>
Tue, 6 Sep 2022 18:18:39 +0000 (14:18 -0400)
committerJason Merrill <jason@redhat.com>
Tue, 6 Sep 2022 20:20:41 +0000 (16:20 -0400)
This usage was intended to be allowed by P2128, but it didn't make it into
the final wording.  Fixed by CWG 2507.

DR2507

gcc/cp/ChangeLog:

* decl.cc (grok_op_properties): Return sooner for C++23 op[].

gcc/testsuite/ChangeLog:

* g++.dg/cpp23/subscript8.C: New test.

gcc/cp/decl.cc
gcc/testsuite/g++.dg/cpp23/subscript8.C [new file with mode: 0644]

index b72b2a8456b2ddc5bff6b67fe5df8884d04a39d6..6d20765f40c80ce527171cb9ab5c71b71616aa7a 100644 (file)
@@ -15331,6 +15331,11 @@ grok_op_properties (tree decl, bool complain)
        "operator ()".  */
     return true;
 
+  /* C++23 allows an arbitrary number of parameters and default arguments for
+     operator[], and none of the other checks below apply.  */
+  if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
+    return true;
+
   if (operator_code == COND_EXPR)
     {
       /* 13.4.0.3 */
@@ -15344,10 +15349,6 @@ grok_op_properties (tree decl, bool complain)
     {
       if (!arg)
        {
-         /* Variadic.  */
-         if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
-           break;
-
          error_at (loc, "%qD must not have variable number of arguments",
                    decl);
          return false;
@@ -15408,8 +15409,6 @@ grok_op_properties (tree decl, bool complain)
     case OVL_OP_FLAG_BINARY:
       if (arity != 2)
        {
-         if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
-           break;
          error_at (loc,
                    methodp
                    ? G_("%qD must have exactly one argument")
diff --git a/gcc/testsuite/g++.dg/cpp23/subscript8.C b/gcc/testsuite/g++.dg/cpp23/subscript8.C
new file mode 100644 (file)
index 0000000..fe00035
--- /dev/null
@@ -0,0 +1,7 @@
+// DR2507: Allow default arguments
+// { dg-additional-options {-std=c++23} }
+
+struct A
+{
+  void operator[](int, int = 42);
+};
This page took 0.088186 seconds and 5 git commands to generate.