Why does the [] operator only take one argument?

There are many questions around the [] operator, only taking one argument, but I can't seem to find one that actually says why.

For example, it seems like a natural extension of the language to matrix[0, 3]

call a function ElementT& operator[](SizeT x, SizeT y)

.

Is there any specific reason (e.g. incompatibility) that this syntax is not in the language, or something other than a lack of motivation actually preventing it from being added?

(Note: this has been marked as a duplicate, but it is not. This question is “why isn't this syntax in the language?” Rather than “how do I solve the problem?”. As mentioned, there are many questions that relate to the latter, but neither one of them doesn't answer first).

+3


source to share


1 answer


Not only is the (comma) operator used in this particular context is a key in works that require a long transition period to get this standardization, we already have another solution that people are using:

ElementT& operator()(SizeT x, SizeT y)

      



Several matrix libraries in the wild use this shape. It's a little ugly, but welcome to C ++. :)

+4


source







All Articles