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;
}
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.
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.
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.
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With