diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 58210905ee9..37d52d8f804 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -15214,6 +15214,9 @@ 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; @@ -15289,7 +15292,8 @@ grok_op_properties (tree decl, bool complain) } /* There can be no default arguments. */ - for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg)) + for (tree arg = argtypes; arg && arg != void_list_node; + arg = TREE_CHAIN (arg)) if (TREE_PURPOSE (arg)) { TREE_PURPOSE (arg) = NULL_TREE; diff --git a/gcc/testsuite/g++.dg/cpp23/subscript7.C b/gcc/testsuite/g++.dg/cpp23/subscript7.C new file mode 100644 index 00000000000..bb811974433 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/subscript7.C @@ -0,0 +1,17 @@ +// PR c++/103460 +// { dg-do compile } +// { dg-options "-std=c++23" } + +struct S { + int &operator[] (int, ...); +} s; +struct T { + int &operator[] (auto...); +} t; +struct U { + int &operator[] (...); +} u; + +int a = s[1] + s[2, 1] + s[3, 2, 1] + s[4, 3, 2, 1] + + t[0.0] + t[nullptr, s, 42] + + u[] + u[42] + u[1.5L, 1LL];