Topic: Why not smart?
Author: Dan Ola Holmsand <dan@et.se>
Date: 1996/04/26 Raw View
This is probably an old question by now, but anyway:
How come the std library only includes auto_ptr, and not any
other "smart pointer" classes?
IMHO, a reference counting smart pointer makes c++ MUCH easier
to use. Of course there are overhead costs, but in most uses
they tend to be negligable.
Moreover, the standard does not seem to oppose very specialized
forms of "smart pointers", most notably the replacement for
char* called string.
My argument for a standard rc_ptr (or whatever) is, I believe,
exactly the same as the argument for a standard string: It makes
sense (at least to me) to be able to pass rc_ptrs between
different frameworks and between functions written by different
people. (I hereby assume that the main purpose of std::string is
not to bring all the find_last_of_but_not_something_else, or
whatever they're called, to the world :-) ).
On the other hand, almost nothing is lost if people pass
different kinds of auto_ptrs around, as I understand the current
implementation. Moreover, implementing an auto_ptr for local use
is trivial.
I know it's no good, but I would have liked an rc_ptr with the
almost same interface as the current auto_ptr in the standard.
In addition, I use an operator T*() for convenience. More
importantly, I use a hash table for storing all the pointers
assigned to any rc_ptr, just to make
rc_ptr<string> x = new string;
string* tmp = x;
rc_ptr<string> y = tmp;
work. (This primarily means that
rc_ptr<Base> x;
rc_ptr<Derived> y;
//....
x = y;
is legal code even in the absense of member template functions).
The overhead is negligable (compared to operator new, at least).
I've also added an is_unique() function.
So, what am I missing? Why string, vector and auto_ptr, and not
rc_ptr (and possibly gc_ptr :-) )?
Dan Holmsand
dan@et.se
---
[ 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 ]