Topic: auto_ptr: no operator bool()?
Author: phalpern@truffle.ultranet.com (Pablo Halpern)
Date: 1996/03/20 Raw View
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 ) ...
>
>You thought you were comparing two pointers, but in fact, you are
>comparing the results of comparing these pointers to null. The
>expression in the if (illegal with the current definition) will cause
>the conversion operator bool to be called for both pointers.
> ... The `obvious' solution for
>auto_ptr is to add an `isValid' function; in the meantime, just use
>`if ( ptr.get() == NULL )'. (Another possible alternative is to
>provide a conversion operator to the underlying pointer type, so that
>you can write `if ( ptr == NULL )', just as you would with a normal
>pointer. I personally don't like the fact that this might result in
>free pointers to the memory without having called a function
>explicitly.)
Why not just have
private:
bool auto_ptr<T>::operator==(const auto_ptr<T>&)
public:
bool auto_ptr<t>::operator==(void *p)
{ return (this->get() == NULL) && (p == NULL); }
The latter would return true if and only if both the auto_ptr and the
passed pointer were NULL.
-------------------------------------------------------------
Pablo Halpern phalpern@truffle.ultranet.com
I am self-employed. Therefore, my opinions *do* represent
those of my employer.
---
[ 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 ]
Author: willer@interlog.com (Steve Willer)
Date: 1996/03/13 Raw View
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 ]