Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comma Operator in subscript operator?

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;
}
like image 801
Nilesh Solanki Avatar asked Jul 20 '20 16:07

Nilesh Solanki


People also ask

What is comma operator in C++?

In the C and C++ programming languages, the comma operator (represented by the token , ) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type); there is a sequence point between these evaluations.

What is the use of comma operator?

The comma operator ( , ) evaluates each of its operands (from left to right) and returns the value of the last operand. This lets you create a compound expression in which multiple expressions are evaluated, with the compound expression's final value being the value of the rightmost of its member expressions.

What is a subscript operator?

The subscript operator is defined as square brackets [] . It is used to access the elements of string, list tuple, and so on. Let's discuss the syntax with an example: <given string>[<index>] The given string is the string you want to examine and the index is the position of the character you want to obtain.

How do you overload a comma in C++?

In C++, we can overload the comma operator using Operator Overloading. For Example: For “Send the query X to the server Y and put the result in variable Z”, the “and” plays the role of the comma. The comma operator (, ) is used to isolate two or more expressions that are included where only one expression is expected.


2 Answers

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.

like image 129
Nicol Bolas Avatar answered Oct 16 '22 21:10

Nicol Bolas


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
like image 2
Öö Tiib Avatar answered Oct 16 '22 21:10

Öö Tiib