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;
}