🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: auto_ptr: no operator bool()?

#3661
From: Steve Willer
Date: Wed, 13 Mar 1996 23:51
86 lines
3640 bytes
James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> wrote:

>The real danger is in the following:
>
>	auto_ptr< T >	p1 ;
>	auto_ptr< T >	p2 ;
>
>	if ( p1 == p2 ) ...

Well, there is obviously a solution of either modifying the way bool
works (so it can't be compared to another), or perhaps creating a new
explicit_bool type.

But what about this solution as a way to implement an auto_ptr:

class explicit_bool {
   bool state_;
   bool operator==(const explicit_bool &rhs) const;
   bool operator!=(const explicit_bool &rhs) const;
public:
   explicit_bool(bool state) { state_ = state; }
   operator bool() { return state_; };
};

template <class T> class auto_ptr {
public:
   /* explicit */ auto_ptr(T *p=0): pointee(p) {}
//  template<class U> auto_ptr(auto_ptr<U> &rhs):
pointee(rhs.release()) {}
   auto_ptr(auto_ptr<T> &rhs): pointee(rhs.release()) {}
   ~auto_ptr() { delete pointee; }
//  template<class U> auto_ptr<T>& operator=(auto_ptr<U> &rhs) {
   auto_ptr<T> &operator=(auto_ptr<T> &rhs) {
      if (this != &rhs) reset(rhs.release());
      return *this;
   }
   T& operator*() const {return *pointee;}
   T* operator->() const {return pointee;}
   T* get() const {return pointee;}
   T* release() {
      T *oldPointee = pointee;
      pointee = 0;
      return oldPointee;
   }
   operator explicit_bool() { return explicit_bool(pointee != 0); }
   explicit_bool operator!() { return explicit_bool(pointee == 0); }
//   operator bool() {return (pointee != 0);}  // apparently not part
of the std
//   bool operator!() {return (pointee == 0);} // ditto
   void reset(T *p=0) {delete pointee; pointee = p;}
private:
   T *pointee;
};

class myclass{};
int main(char,char**) {
   auto_ptr<myclass> a1,a2;

   a1 = a2;
   a1 == a2;
   a1 != a2;
   if (a1);
   if (!a1);

   return 0;
}

To tell you the truth, I'm not 100% sure this would work properly,
because I don't have my C++ books here (I'm at home sick), and my
compiler doesn't have "bool" as a built-in type (it's a class). With
my current compiler, the "a1==a2" and "a1!=a2" lines don't work
(illegal structure operation), which is exactly how I'd expect it. The
"if (a1)" line doesn't compile either, but wouldn't it work if "bool"
was a built-in type? This would mean an implicit conversion to
explicit_bool and then to bool, but my (somewhat faulty)  memory is
that that's legal.

Even if that's not legal, what about defining "template <class T>
template<class U>auto_ptr<T>::operator==(const auto_ptr<U>& rhs)" and
the equivalent operator!= as private?
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Technical Details
Message-ID: <31474cd9.10139718@news.interlog.com>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <313ddfd9.16044605@sqarc.sq.com> <4i2jqr$8dl@solutions.solon.com>

Show Full Headers
Complete Article Headers
From: willer@interlog.com (Steve Willer)
Message-ID: <31474cd9.10139718@news.interlog.com>
X-Original-Date: Wed, 13 Mar 1996 23:02:38 GMT
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 13 Mar 96 23:51:30 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: Re: auto_ptr: no operator bool()?
Organization: InterLog Internet Services
References: <313ddfd9.16044605@sqarc.sq.com> <4i2jqr$8dl@solutions.solon.com>
X-Newsreader: Forte Agent .99d/32.182
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMUdfkeEDnX0m9pzZAQH2uwF+Icw5iWYz8a+Hpaq0j7UrXEG3euLSNN6/
 jiPMFV3llZbbvfqetuNOE5WM03j+B/6c
 =VgJR