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 :
- Why is the comma operator called inside an
operator[]
call and not inside anoperator()
call ? - 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 any f declaration, would not try to change the comma status and see iff(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;
}
operator()
is called the "function call operator" as it is used (and parsed) as that. – Bioperator,
could decide whetherz(x,y)
had one or two arguments. – Oahuf
to consider in the presence of overloads. And what to think aboutf(x,y,z)
whenf
takes two parameters - do you try bothf(x,(y,z))
andf((x,y),z)
? – Copleyf(a, b, c, d, e ,f);
which is the comma-operator and which is the parameter-list-separator? Too hard for the compiler and to be backwards compatible they are all parameter-list-separator(s). Knowing that now concider:g((a, b), (c), (d, e, f));
You should see thatf
takes 6 parameters andg
takes only 3. With the extra commas (inside the extra brackets) ing
being operators (the brackets aroundc
are optional). – Hedjazf(int)
andf(int,int)
would need special rules to preventf(1,2)
form being ambiguous. – Copleyoperator[]
will have same properties as function since multidimensional subscript has been introduced. – Needleful