6

I am quite confused with the comma operator. I have never seen such code with such syntax? but I am curious if it useful at any place? why is it deprecated in c++20?

#include <iostream>
int main()
{
    int  a[5]{1,2,3,45,5};
    std::cout << a[(2,3)] <<'\n';  //  this is work , in c++17 works
    std::cout << a[2,3] << '\n';   // but this is deprecated in c++20 ,in c++17 works
    return 0;
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
Nilesh Solanki
  • 336
  • 4
  • 19
  • 4
    So what research did you do? Did you read the proposal? – KamilCuk Jul 20 '20 at 16:48
  • @KamilCuk yeah, I was checking the operator on cppreference website. I got this syntax. I didn't understand. Why is it working? why it's resulting 45, why not 3? i want to understand in such a way. what is the purpose of such a thing in real life? – Nilesh Solanki Jul 20 '20 at 16:54
  • i didn't understand written thing on.https://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator – Nilesh Solanki Jul 20 '20 at 16:55
  • Sounds familiar: https://www.youtube.com/watch?v=qD1aKLux5Fw – Bob__ Jul 20 '20 at 17:00
  • @Bob_ but still I didn't understand why it is not printing 3.why in world people will like to use – Nilesh Solanki Jul 20 '20 at 17:15
  • 2
    Please note that indeces start from 0. So that in `2, 3` 2 is "ignored" and the element at *index* 3 (which is 45) is selected. – Bob__ Jul 20 '20 at 17:28
  • the comma operator is explained in every C++ book, [did you read one](https://stackoverflow.com/q/388242/995714)? There are very few [usage for the the comma operator](https://stackoverflow.com/q/8219836/995714) and even fewer [clever ways to overload it to do something useful](https://stackoverflow.com/q/5602112/995714) and deprecating it makes it far more practical in the future – phuclv Jun 10 '22 at 14:32

2 Answers2

13

It's important to recognize the difference between comma as an expression operator and comma as a separator between grammatical terms of some kind. They use the same punctuation, but they don't have the same meaning.

Within the {} of a braced-init-list, individual terms are separated by commas. So {1,2,3,45,5} is a sequence of terms. That's a comma-as-separator.

However, within a general expression, the comma acts as an expression operator. When a comma is an expression operator between two expression terms, it means to evaluate the left expression, discard its result, then evaluate the right expression, which is the result of the total expression.

Within a [], a comma is not a separator in C++17. Therefore, it acts as an expression operator. a[2,3] means to evaluate 2, discard it, then evaluate 3. So the index used will be 3.

C++20 deprecates a comma expression as the direct expression used in a []. It does this so that future versions of the C++ standard will have the freedom to make commas within [] become comma separators rather than comma operators. That is, [2, 3] makes 2 and 3 the parameters to a call to an overloaded operator[].

This is similar to how the parameters to a function use the separator comma. So if you need to use the operator comma on two expressions within a function call, you have to wrap them in (): func(1, (2, 3)). This function takes two parameters, with the second one being the result of the comma operator applied to its terms.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
3

Prime reason why it is deprecated is that it is hoped to use the syntax in the future for multidimensional subscript operators.

Three other reasons why it is deprecated you already said:

  • It is very rarely used in practice
  • People are often confused when it is used what is going on
  • People are in difficulty to think why it is allowed
Öö Tiib
  • 10,809
  • 25
  • 44