🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Implementing auto_ptr + type of operator->*

#1927
From: Rogier Goossens
Date: Fri, 08 Sep 1995 21:14
110 lines
4069 bytes
> I've been trying to implement the auto_ptr template of section 20.4.5 of
> the draft standard, and, much to my surprise, this seems to be a compiler
> buster.  Borland 4.51, MetaWare 3.3, and g++ 2.6.3 all generate errors
> either during compilation or linking.

With g++ 2.7.0 I got:

try.cc: In function `int main()':
try.cc:15: cannot convert type `Foo *' to type `auto_ptr<Foo> &'
auto_ptr.h:5: in passing argument 1 of `auto_ptr<Foo>::auto_ptr(auto_ptr<Foo> &)'
ptr_try.cc:15: in base initialization for class `auto_ptr<Foo>'

Line 15 is the line p1 is defined in.
Although I do not quite understand why, I have been told this behavior is
correct. It is a result of the constructor being explicit.
After changing that line to :

    auto_ptr<Foo> p1(new Foo(1,2,10));

Your code compiles OK and outputs:

    1 2 10


I have two more remarks:

>   template<class T>
>   inline void operator=(auto_ptr<T>& rhs)
>   { ptr = rhs.release; }

Don't forget to delete the previous object ptr pointed to.

>   template<class T>
>   inline auto_ptr<T>::~auto_ptr() { delete ptr; }

I'm not sure whether 'delete ptr' is legal when ptr==NULL. If not,
you'd better check.

	[There is no need to check.  `delete ptr' is guaranteed to have
	no effect when ptr==NULL.  -fjh (moderator).]

I have another, related question:

For a 'pointer' type to be complete, it seems to me it needs an 'operator ->*'
as well.

How would such an operator be declared for a pointer<C> class ?

One possibility is:

    template<class C>
    class pointer {
	public:
	template<class TMC> 	// TMC : type of member of class C
	TMC &pointer<C>::operator ->*(TMC *cmp);    // cmp: C member pointer
	}

This looks like the declaration from section 13.6.11 of the C++ draft
standard :

    CV12 T& operator->*(CV1 C*, CV2 T C::*);

which holds for non-class types.
(CV* are 'cv-qualifier-seqs', T is a complete object or function type,
and C is a class type.)


The semantics would then have to be something like:

    pointer<myclass> p;
    ...
    p->*memberptr;	    <-->  p->(p.operator->*(memberptr));
    p->*methodptr(...);	    <-->  p->(p.operator->*(methodptr))(...);

The alternative:

    p->*memberptr;	    <--> p.operator->*(memberptr);

only works for non-method member pointers: operator->* retuns a member
reference which allows it to compute the actual member that should be
used/invoked.  For pointers to non-method members, operator->* could also
compute the actual object to use (like operator-> does), and return a
reference to the requested member of that object (1).  For method pointers,
this is not possible, as it doesn't know which arguments to use in the
invocation.

(1): This may not always be legal though. It may require a conversion from
     member pointer to regular pointer (e.g. int C::* -> int *), which is not
     allowed as far as I know.


Although the draft standard does mention how a user-defined operator-> works
(in section 13.5.6), it does not mention anything about a user-defined
operator->*. The only thing I found was is section 13.6.11 (see above), which
only applies to built-in operators.


To resume:
- Is such a declaration for operator->* possible at all, and if so, is
  the declaration above correct.
- If it is not possible, can a 'real' pointer class be written at all ?
- If it is possible, are 'my' semantics of operator->* correct.


Rogier
goossens@cs.few.eur.nl
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <9509080952.AA15973@jodium.cs.few.eur.nl>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 08 Sep 95 21:14:46 GMT
Approved: fjh@cs.mu.oz.au
Organization: -
Newsgroups: comp.std.c++
From: Rogier Goossens <goossens@cs.few.eur.nl>
X-Original-Date: Fri, 8 Sep 95 11:52:40 +0200
Message-ID: <9509080952.AA15973@jodium.cs.few.eur.nl>
Subject: Re: Implementing auto_ptr + type of operator->*