From this former question When all does comma operator not act as a comma operator?, I understood that commas inside a function call can only act as expression sperator. But from the code below, it appears that operator()
behaves like a function call while operator[]
does not.
So I have two questions :
operator[]
call and not inside an operator()
call ?f(a,b)
does not match both the arity or the types of any f declaration, would not try to change the comma status and see if f(a.operator,(b))
leads to an acceptable synthax ? From my point of view, it would be the same kind of process that comes with types conversion.Code example :
struct A{ };
struct B {
A operator,(const B & other) const { return A(); }
};
struct C {
C(){}
C(const A & a){}
void operator[](const A & a) const {}
void operator()(const A & a) const {}
};
void f(const A & a){}
int main()
{
B x,y;
C z;
//these do no compile because ',' in a function call is an argument separator
//C(x,y);
//f(x,y);
//but this one compiles as z[x.operator,(y)]
z[x,y];
//and this one does not
//z(x,y);
//finally all of these do compile
z((x,y));
C((x,y));
f((x,y));
return 0;
}
The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.
1) Comma as an operator: The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator, and acts as a sequence point.
JavaScript uses a comma ( , ) to represent the comma operator. A comma operator takes two expressions, evaluates them from left to right, and returns the value of the right expression. In this example, the 10, 10+20 returns the value of the right expression, which is 10+20. Therefore, the result value is 30.
A comma operator (,) in JavaScript is used in the same way as it is used in many programming languages like C, C++ etc. This operator mainly evaluates its operands from left to right sequentially and returns the value of the rightmost operand.
Why is the comma operator called inside an
operator[]
call and not inside anoperator()
call?
If you look grammatically, function calls are of the form postfix-expression (
expression-listopt)
. An expression-list (which is an initializer-list, not to be confused with std::initializer_list
) is a comma separated list of initializer-clauses (assuming there are at least two clauses). The commas are consumed by the parsing of the expression-list, where it has special meaning, rather than an part of an expression.
Indexing is of the form postfix-expression [
expr-or-braced-init-list ]
, there is no comma to be consumed at this point, so any comma that appears is necessarily part of the expression.
Is there a specific reason that prevents the compiler, first checking that
f(a,b)
does not match both the arity or the types of anyf
declaration, would not try to change the comma status and see iff(a.operator,(b))
leads to an acceptable syntax ?
I'm going to go with "sanity." Function calls are a really fundamental aspect of programs and they need to be straightforward. It would be insanely error-prone if you couldn't even know how many arguments you were passing. Especially if the builtin comma operator is used, which simply ignores arguments.
Moreover, it's very simple to force the use of a comma: add parentheses:
f(a, (t=3, t+2), c);
has three arguments, the second of which has value
5
.
This works grammatically because the interior comma can't be a comma separating initializer-clauses, since (t=3
is not an initializer-clause.
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