Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does operator[] only take one argument? [duplicate]

There are plenty of questions related to operator[] only taking one argument, but I can't find one that actually says why.

For example, it seems a very natural extension of the language to have matrix[0, 3] call an ElementT& operator[](SizeT x, SizeT y) function.

Is there any particular reason (e.g. incompatibility) that this syntax isn't in the language, or anything besides lack of motivation actually preventing it from being added?

(Note: This has been marked a duplicate, but it is not. This question is "why isn't this syntax in the language?" not "how do I work around the issue?". As mentioned, there are plenty of questions that address the latter, but none that answer the former.)

like image 319
user673679 Avatar asked Sep 29 '22 15:09

user673679


1 Answers

Not only is the (little-used in this particular context) comma operator a wrench in the works which require a lengthy transition period to get this standardized, we already have another solution which people use:

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

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

like image 103
John Zwinck Avatar answered Oct 04 '22 02:10

John Zwinck