Article View: comp.std.c++
Article #2106Re: auto_ptr: :reset and auto_ptr::operator=
From: Dietmar Kuehl
Date: Wed, 04 Oct 1995 07:17
Date: Wed, 04 Oct 1995 07:17
80 lines
3585 bytes
3585 bytes
J. Kanze (kanze@gabi-soft.fr) wrote:
: Ideally, perhaps, I would want to have assignment and copy construction
: defined only when the source is a temporary. In practice, in my own
: code, I find that in fact, all (or almost all) of the assignments and
: copies are in fact from temporaries. (Off hand, I cannot remember any
: that wasn't either a function return value, or a parameter to a
: function.) So I find that my solution, while ugly WRT C++ programming,
: does correspond to what I expect an auto_ptr to do.
The statement with the temporary gave me the following idea: Using two
classes instead of just one class, it is possible to protect the copy
constructor and the assignment operator while still allowing a function
to return a "safe" object.
The two class are auto_ptr and auto_ptr_tmp. They have the following
properties:
- auto_ptr_tmp has a public destructor
- all other functions of auto_ptr_tmp are private
- auto_ptr is a friend of auto_ptr_tmp
- auto_ptr has a private assignment operator and copy constructor
- auto_ptr has a constructor and assignment operator taking
a auto_ptr_tmp
- auto_ptr has a conversion operator to auto_ptr_tmp
- everything else is like auto_ptr in the DWP
Using this setup would yield the following:
auto_ptr_tmp<foo> f1()
{
auto_ptr<foo> ap(new foo());
return ap; // conversion to auto_ptr_tmp
}
void f2(auto_ptr<foo>); // illegal: copy constructor is private
auto_ptr<foo> f3(); // legal, but (almost) useless
h()
{
auto_ptr<foo> a1;
auto_ptr<foo> a2(new foo);
auto_ptr<foo> a3(a2); // illegal: copy constructor is private
a1 = a2; // illegal: assignment operator is private
(auto_ptr_tmp<foo>)a1; // legal: "steal" the pointer of a1
a1 = (auto_ptr_tmp<foo>)a2; // legal: conversion to auto_ptr_tmp and
// assignment using auto_ptr_tmp :-(
a1 = (auto_ptr_tmp<foo>)f3; // legal: conversion to auto_ptr_tmp and
// assignment using auto_ptr_tmp :-(
f(); // ~auto_ptr_tmp() takes care of pointer
a1 = f(); // legal: assignment taking auto_ptr_tmp
auto_ptr<foo> a4(f()); // legal: constructor taking auto_ptr_tmp
}
However, some "ugly" parts are present:
- functions can only return a auto_ptr_tmp (without casting)
- it is still possible to assign auto_ptrs using explicit casts
- there is still some const/mutable "hackery" to be able to
modify a returned auto_ptr_tmp
- using a cast it is possible to "steal" a pointer which is
unexpected
Although there are ugly parts these two classes prohibit all straight
forward uses which are allowed with auto_ptr and which lead to
unexpected behavior.
dk
--
http://www.informatik.uni-konstanz.de/~kuehl
dietmar.kuehl@uni-konstanz.de
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ 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:
<44so2o$2h0@eurybia.rz.uni-konstanz.de>Path:
arc1-ca.pugleaf.net!howland.reston.ans.net!agate!agate!MattReferences:
<44oenv$c6i@gabi.gabi-soft.fr>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!howland.reston.ans.net!agate!agate!Matt From: kuehl@uzwil.rz.uni-konstanz.de (Dietmar Kuehl) Newsgroups: comp.std.c++ Subject: Re: auto_ptr: :reset and auto_ptr::operator= Date: 04 Oct 1995 07:17:34 GMT Organization: Fakultdt f|r Mathematik und Informatik Approved: Matt@Austern Message-ID: <44so2o$2h0@eurybia.rz.uni-konstanz.de> References: <44oenv$c6i@gabi.gabi-soft.fr> Reply-To: Dietmar.Kuehl@uni-konstanz.de NNTP-Posting-Host: austern.hip.berkeley.edu