Topic: Suggestion: ->[], ->()


Author: "Richard Smith" <richard@ex-parrot.com>
Date: Wed, 3 Apr 2002 20:16:39 GMT
Raw View
"Sungbom Kim" <musiphil@bawi.org> wrote in message
news:3CAA5E13.7015A029@bawi.org...
>
> Wouldn't it be nice if instead of saying (*p)[n] or p->operator[n]
> we could just say p->[n], and instead of saying (*p)(...) or
> p->operator(...), p->(...)? (Here p would be a pointer or an iterator
> to a type to which operator[] or operator() can be applied.)

I personally think this would be an extremely good thing.  I would use it
myself, and I think it could do a lot to clear up certain types of code.

> This syntactic sugar improves readability and conciseness of the code,
> without breaking any existing code or requiring any changes to existing
> codes. Hopefully a slight change of the language syntax rules would do.

It is complicated by operator overloading.  With built-in types (*foo).x and
foo->x are the same and so whether you interpret foo->[n] as
foo.operator->()->operator[](n) or as foo.operator*().operator[](n) is
irrelevant, but this difference becomes important if -> and * are both
overloaded.  (I would argue that the former is prefereable as this is closer
to what is written, plus you get operator-> forwarding.)

Then, do you allow the overloading of operator->() and ->[] in their own
rights?  (I personally can't see any reason why you'd need to, but maybe I'm
being insufficiently imaginative this morning.)  If so do you supply all
classes with user-defined operator-> but no user-defined operator->()
or ->[] with default ones?  (I think if you did allow overloading of
operator->() and ->[] then you probably should provide default versions so
that existing iterator and smart pointer classes could immediately cash in
on this.)

--
Richard Smith


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: remove.haberg@matematik.su.se (Hans Aberg)
Date: Thu, 4 Apr 2002 20:14:05 GMT
Raw View
In article <3CAA5E13.7015A029@bawi.org>, Sungbom Kim <musiphil@bawi.org> wrote:
>Wouldn't it be nice if instead of saying (*p)[n] or p->operator[n]
>we could just say p->[n], and instead of saying (*p)(...) or
>p->operator(...), p->(...)? (Here p would be a pointer or an iterator
>to a type to which operator[] or operator() can be applied.)

I worked on operators with precedence in way that they can capture at
least C++ expression. For example, an operator like x->[y] would be
classified as a binary postfix operator (middle delimiter "->[" and last
delimiter "]"). (There were some posts about this in this group.)

There are unfortunately some problems when working in this generality that
I do not know yet how to solve. (Namely, how to combine systems of
operators with precedence, and the question of name overloading of the
different operator components.)

  Hans Aberg      * Anti-spam: remove "remove." from email address.
                  * Email: Hans Aberg <remove.haberg@member.ams.org>
                  * Home Page: <http://www.matematik.su.se/~haberg/>
                  * AMS member listing: <http://www.ams.org/cml/>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Sungbom Kim <musiphil@bawi.org>
Date: Wed, 3 Apr 2002 03:11:50 GMT
Raw View
Wouldn't it be nice if instead of saying (*p)[n] or p->operator[n]
we could just say p->[n], and instead of saying (*p)(...) or
p->operator(...), p->(...)? (Here p would be a pointer or an iterator
to a type to which operator[] or operator() can be applied.)

This syntactic sugar improves readability and conciseness of the code,
without breaking any existing code or requiring any changes to existing
codes. Hopefully a slight change of the language syntax rules would do.

Inspired by Perl, which allows things like $arrayref->[], $hashref->{},
and $coderef->(), etc.

I would like to hear what you think of this idea.

--
Sungbom Kim <musiphil@bawi.org>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]