Topic: auto_ptr implemented without member templates
Author: Cristian Georgescu <Cristian.Georgescu@worldnet.att.net>
Date: 1998/03/22 Raw View
It seems that MS VC++ 5.0 does not support member templates.
I also think that the implementation for the auto_ptr that comes with it
is not quite working.
Therefore, I have tried to rewrite the auto_ptr without using member
templates
(and without modifying it to much); The code that was removed was
commented with //-.
The code that was added is commented. I would appreciate any feedback on
it,
I am not quite sure I have got it right.
I add here the code. (the code was adapted from "More Effective C++
35 New Ways to Improve Your Programs and Designs" by: Scott Meyers).
///////////////////////////////////////////////////////////////////////////////
template<class X>
class auto_ptr {
mutable bool owner;
X* px;
// template<class Y> friend class auto_ptr;
public:
// explicit removed so that implicit conversion can take place
//- explicit auto_ptr(X* p=0)
auto_ptr(X* p=0)
: owner(p!=0), px(p) {}
// template members not supported
//- template<class Y>
//- auto_ptr(const auto_ptr<Y>& r)
auto_ptr(const auto_ptr& r)
: owner(r.owner), px(r.release()) {}
// template members not supported
//- template<class Y>
//- auto_ptr& operator=(const auto_ptr<Y>& r) {
auto_ptr& operator=(const auto_ptr& r) {
if ((void*)&r != (void*)this) {
if (owner)
delete px;
owner = r.owner;
px = r.release();
}
return *this;
}
// Added overloaded assignment to auto_ptr from a ptr
auto_ptr& operator=(X* p) {
return operator=(auto_ptr<X>(p));
}
~auto_ptr() { if (owner) delete px; }
X& operator*() const { return *px; }
X* operator->() const { return px; }
X* get() const { return px; }
X* release() const { owner = 0; return px; }
// Added conversion operator to a ptr
operator X*() {release(); return px;}
};
///////////////////////////////////////////////////////////////////////////////
// Using auto_ptr
///////////////////////////////////////////////////////////////////////////////
class A {
public:
int at;
virtual ~A(){}
};
class B : public A {
public:
int bt;
virtual ~B(){}
};
///////////////////////////////////////////////////////////////////////////////
void main()
{
auto_ptr<A> pa ( new A );
pa->at = 1;
auto_ptr<B> pb ( new B );
pb->at = 2;
pb->bt = 22;
pa = pb;
// pb = pa; // does not work - is OK!
///////////////////////////////////////////////////////////////////////////////
char ch;
cout << endl << ">> Press any key to EXIT: "; cin >> ch;
}
///////////////////////////////////////////////////////////////////////////////
--
Cristian Georgescu
_________________________________________________
Smiths Industries
Aerospace & Defense Systems
7-9 Vreeland Road,
Florham Park, NJ 07932, USA.
_________________________________________________
E-mail: Georgescu_Christian@si.com
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "John D. Hickin" <hickin@Hydro.CAM.ORG>
Date: 1998/03/23 Raw View
THe auto_ptr that ships with VC5 has a bug which allows to auto_ptr
instances to own the same object with disasterous results. Try the
following program on your implementation to see if you got it right.
Regards, John.
//-----
/*
** Problem: auto_ptr template causes double deletion of contained
object.
** Severity: severe
** Work-around: none
**
** Test Case: Create an auto ptr instance with a contained object.
** Create a second auto_ptr instance using copy constructor.
** Assign the first instance from the second.
** Result: Two auto_ptr instances own the same datum; heap is
corrupted
** when the datum is deleted a second time.
**
** Auto_Ptr: This template, copied from Vc/include/memory, was modified
** slightly to prevent the double delete. It is NOT compliant
** with the standard (IMHO) but this implementation has the
** following great advantage:
** The assignment x = y is idempotent
*/
#include <iostream>
#include <memory>
#include <Auto_Ptr.h>
#include <Auto_Ptr_New.h>
using namespace std;
struct T
{
T() { cerr << "T::T, this==" << (void*)this << endl; }
~T() { cerr << "T::~T, this==" << (void*)this << endl; }
};
void test_AP()
{
Auto_Ptr<T> p( new T );
Auto_Ptr<T> q( p );
p = q;
}
void test_ap()
{
auto_ptr<T> p( new T );
auto_ptr<T> q( p );
p = q;
}
void test_APN()
{
Auto_Ptr_New<T> p( new T );
Auto_Ptr_New<T> q( p );
p = q;
}
int main()
{
cerr << "==> test " << typeid(Auto_Ptr<T >).name() << "\n";
test_AP ();
cerr << "==> test " << typeid(Auto_Ptr_New<T >).name() << "\n";
test_APN ();
cerr << "==> test " << typeid(auto_ptr<T >).name() << "\n";
test_ap ();
return EXIT_SUCCESS;
}
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: menright1@home.net (Mike Enright)
Date: 1998/03/23 Raw View
Cristian Georgescu <Cristian.Georgescu@worldnet.att.net> wrote:
>It seems that MS VC++ 5.0 does not support member templates.
I have used member templates in that compiler. Have you applied (at
least) Service Pack 2?
>I also think that the implementation for the auto_ptr that comes with it
>is not quite working.
That's a different matter, and I don't know either way. It could be
that if you applied SP2 auto_ptr would work.
><MEC++ auto_ptr snipped>
[crossposted and followups set to microsoft.public.vc.language]
--
Mike Enright
menright@cts.com (Email replies cheerfully ignored, use the news group)
http://www.users.cts.com/sd/m/menright/
Cardiff-by-the-Sea, California, USA
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]