Multi-Argument Array Operator in C ?
In C , operators can be overloaded to provide custom behavior for built-in operators such as []. However, prior to C 23, overloading [] to accept multiple arguments was not possible.
Limited Support Before C 23
Attempts to define a multi-argument [] operator would result in a compiler error, as seen in the example code:
const T& operator[](const int i, const int j, const int k) const { return m_cells[k*m_resSqr+j*m_res+i]; } T& operator[](const int i, const int j, const int k) { return m_cells[k*m_resSqr+j*m_res+i]; }
This code will trigger the error:
error C2804 binary operator '[' has too many parameters
Workaround Before C 23
As a workaround, one could overload the () operator instead:
T& operator()(const int i, const int j, const int k) { return m_cells[k*m_resSqr+j*m_res+i]; }
Support in C 23
From C 23, the language includes support for multi-argument [] operators. As a result, the code shown earlier would be valid and would allow multiple subscript arguments to be passed directly to the [] operator.
The above is the detailed content of How Does C Handle Multi-Argument Array Operators?. For more information, please follow other related articles on the PHP Chinese website!