-1

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?

digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • 1
    Can you provide a minimum reproducible example? – paolo Jun 10 '22 at 09:43
  • 2
    Please provide a [mcve]. In particular, it is not clear what you mean by "overloaded the 2D subscript operator" - I cannot reproduce this warning with code that actually provides something like `operator[](int, int)`, only on code that provides `operator[](int)`. – Barry Jun 10 '22 at 15:07
  • @Barry Yes, I tried my MRE on compiler explorer with GCC 12.1 but the warning did not appear. Maybe my compiler shows the warning because it's not 12.1 but a previous version (experimental) of GCC 12. – digito_evo Jun 10 '22 at 22:33
  • @Barry Oh no! I discovered a bug in my program and the compiler's warning is totally correct! I used the 2D operator on a vector and not on an instance of that class. How dumb! – digito_evo Jun 10 '22 at 22:44

2 Answers2

2

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.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
2

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

Krzysztof Lewko
  • 982
  • 7
  • 24