warning: top-level comma expression in array subscript changed meaning in C++23 [-Wcomma-subscript]

533 views Asked by At

I have overloaded the 2D subscript operator in one of my classes. And for that I use the -std=c++23 option to compile the program.

Now when calling this operator, GCC complains:

warning: top-level comma expression in array subscript changed meaning in C++23 [-Wcomma-subscript]
  331 |                 m_characterMatrix[ x1, y1 ] = ch.value( );
      |                 ~~~~~~~~~~~~~~~~~^

So what is this warning for? Should I take it seriously?

2

There are 2 answers

0
molbdnilo On BEST ANSWER

The warning is there because the compiler's assumption is that you might have been expecting the pre-C++23 behaviour - that is, the "traditional" comma operator evaluation.
(While common sense would clearly indicate that you meant to use your overload and there is no problem, computer programs don't possess common sense.)

You can disable that warning with -Wno-comma-subscript.

0
Krzysztof Lewko On

Using an unparenthesized comma expression as second (right) argument of a subscript operator is deprecated.

For example, a[b, c] is deprecated and a[(b, c)] is not. An unparenthesized comma expression cannot be second (right) argument of a subscript operator. For example, a[b, c] is either ill-formed or equivalent to a.operator[](b, c). Parentheses are needed to for using a comma expression as the subscript, e.g., a[(b, c)].

Via: https://en.cppreference.com/w/cpp/language/operator_other

So yes, I think you should add parens inside operator[] to have old behaviour