🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #676

Re: Operator -> cannot return non-class types?

#676
From: James Kanze
Date: Fri, 13 Nov 1992 19:16
72 lines
3342 bytes
In article <1992Nov11.092316.306@aut.abb.se>, dlarsson@aut.abb.se
(Daniel Larsson) writes:

|> > To my surprise, I noticed that operator -> can only return the following
|> > three types:
|> >    - a pointer to a class,
|> >    - a class, or
|> >    - a reference to a class.
|> > 
|> > This means I cannot do simple things like:
|> > 
|> >   template< class T >
|> >   class OffsetPtr 
|> >     {
|> >     public:
|> >       OffsetPtr();
|> >       OffsetPtr( T* );
|> >       ...
|> >       T* operator ->();
|> >       ...
|> >     };
|> > 
|> >   OffsetPtr<char> ptrToString;
|> > 
|> > What is the rationale behind this restriction? I couldn't find anything in
|> > C++PL 2nd ed. that mentions this restriction, although I didn't look too hard.

|> a->b
|>   ...where a is of a class type is interpreted as...
|> (a.operator->())->b
|>   ...which in turn may expand to...
|> ((a.operator->()).operator->())->b
|>   ...and so on.

|> but in all cases the thing on the rhs of the -> is a class member.
|> Since types other that structs and classes do not have members it
|> would be useless for operator->() to return anything other than a
|> pointer to a class or a class for which operator->() is also defined.

|> Now what you could do is define operator T* (). Unfortuneately if you
|> write `a->b' where a is of a class without an operator->() the
|> compiler does not try compiling it as `(*a).b' so you'll have to write
|> this out in full each time. Likewise you can't write `a[5]' but you
|> can write `*(a+5)'.

|> This has annoyed me in the past and I feel that the language
|> definition should employ user defined type conversions to resolve the
|> lh operand of the -> ->* and [] operators.

IMHO, this should only be a problem in templates.

Basically, I don't offhand see anything in the language definition
that would restrict an operator->() (the function) from returning
whatever it wishes.  On the other hand, there is no way to use this
operator *unless* it returns a pointer to a class.  This is no doubt
why your compiler introduced the restriction.

Normally, of course, you would never want to write a function that
could not be called.  However, in the case of templates, it is
possible that you might, since it could be called for the
instantiations using a class type, and any attempt to use it for the
instantiations using a non-class type would cause a compiler error at
the point of use.

(Actually, you can call the function, even for a non-class type
result, explicitly: "c = operator->( p ) ;", for example.  However, I
don't think that this is really the idea behind operator overloading.)
--
James Kanze			GABI Software, Sarl.
email: kanze@us-es.sel.de	8 rue du Faisan
				67000 Strasbourg
				France
Technical Details
Message-ID: <KANZE.92Nov13191617@slsvhat.us-es.sel.de>
Path: arc1-ca.pugleaf.net!sparky!uunet!snorkelwacker.mit.edu!ira.uka.de!ira.uka.de!slsvaat!josef!kanze
References: <1992Nov11.092316.306@aut.abb.se> <1992Nov11.184341.1@vax1.bham.ac.uk>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++,comp.lang.c++
Path: arc1-ca.pugleaf.net!sparky!uunet!snorkelwacker.mit.edu!ira.uka.de!ira.uka.de!slsvaat!josef!kanze
From: kanze@us-es.sel.de (James Kanze)
Subject: Re: Operator -> cannot return non-class types?
In-Reply-To: mccauleyba@vax1.bham.ac.uk's message of Wed, 11 Nov 1992 18:43:41 GMT
Message-ID: <KANZE.92Nov13191617@slsvhat.us-es.sel.de>
Sender: news@us-es.sel.de
Organization: SEL
References: <1992Nov11.092316.306@aut.abb.se> <1992Nov11.184341.1@vax1.bham.ac.uk>
Date: 13 Nov 92 19:16:17
Lines: 72