🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: auto_ptr: no operator bool()?

#3744
From: Pablo Halpern
Date: Wed, 20 Mar 1996 03:17
45 lines
2452 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 ) ...
>
>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                             ]
Technical Details
Message-ID: <314ec393.5364744@news.ultranet.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: phalpern@truffle.ultranet.com (Pablo Halpern)
Message-ID: <314ec393.5364744@news.ultranet.com>
X-Original-Date: Tue, 19 Mar 1996 15:30:16 GMT
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 20 Mar 96 03:17:31 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: Re: auto_ptr: no operator bool()?
Organization: UltraNet Communications, Inc.
References: <313ddfd9.16044605@sqarc.sq.com> <4i2jqr$8dl@solutions.solon.com>
X-Newsreader: Forte Agent .99d/16.182
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMU942uEDnX0m9pzZAQFKgQF9Hy2w2YVlB+LLxTxut+06Xnt6oc0s5jVs
 7v9cqrmplF6T4eXeg9V7NyRJnLcW7nv+
 =s7yK