5

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.)

user673679
  • 1,327
  • 1
  • 16
  • 35
  • 3
    Probably something to do with `a[1,3]` currently being perfectly valid syntax already (the `,` being the comma operator). – T.C. Oct 11 '14 at 14:35
  • https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/tOJx2TcHpH0 – chris Oct 11 '14 at 14:35
  • Operator overloading in C++ can't really change the basics of how the overloaded operators work, and there is no operator `[]` in C++ which takes multiple values. – Some programmer dude Oct 11 '14 at 14:35
  • @JoachimPileborg: Aside from short-circuiting and sequencing, which overloading `,`, `&&` and `||` removes. Which can really throw people. – Deduplicator Oct 11 '14 at 14:52
  • 3
    @FredOverflow This question is not a duplicate. I have explained why. – user673679 Oct 11 '14 at 16:25
  • Then way you're asking now, it's off-topic. SO is not about discussing the designs of programming languages. Actually, it's not about discussing at all. – lxg Oct 11 '14 at 16:56
  • 3
    I asked a specific question about a specific design feature of a specific language, which has been provided with a definite answer. It wasn't simply a topic for discussion, and it's not a duplicate. – user673679 Oct 11 '14 at 17:29

1 Answers1

4

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++. :)

John Zwinck
  • 239,568
  • 38
  • 324
  • 436