Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't ->[] an operator in C++? (a subscript operator for dereferenced array pointers) [closed]

I know asking Why? is a bad question for this site, since we can't know. However I mean it as a colloquial replacement for asking, What are some possible reasons?

I found myself writing, naturally,

foo->[i];

and being surprised to learn that it didn't work. I meant to write:

(*foo)[i];

I'm sure most see already what I mean, but to be clear, I thought

bar.subscript(i);
foo->subscript(i);

would be analogous to

bar.operator[](i);
foo->operator[](i);

But this doesn't seem to be the case. Why? I'm certain I must be looking at something the wrong way, but I can't figure out what. I know very little theory, so a layperson's explanation would be appreciated.

If there's no obvious error in my analogy though, then what are some possible reasons the designers of the language may have left the operator out? Is it ambiguous? (If so, as being mistakable for what?)


I'd like to bring some comments into an edit, as per @chris's recommendation, as I have not been as clear as I should have been:

The OP is proposing operator->[], a combination of the two.
– chris
He's asking why the thing he wants doesn't exist, not why the code he's trying to write doesn't work.
– Matthew 'Cogwheel' Orlando
like image 819
slackwing Avatar asked Jan 04 '13 19:01

slackwing


1 Answers

The right of operator-> must be the name of a member. An explicit operatorXYZ counts as the name of a member I guess, so foo->operator[] is allowed. However, a random token such as [] doesn't count as the name of a member, so foo->[i] is not allowed.

like image 54
user1610015 Avatar answered Oct 24 '22 07:10

user1610015