Re: shared_ptr and nullptr
On Nov 6, 5:50 pm, jgott...@carolina.rr.com (Joe Gottman) wrote:
But shared_ptr's operator== is defined as follows:
template<class T, class U>
bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b);
So even if we do give shared_ptr a constructor that takes a nullptr,
will the above operator==() function get called?
We might have to define the following 4 functions:
template <class T> bool operator==(const shared_ptr<T> &, nullptr_t);
template <class T> bool operator!=(const shared_ptr<T> &, nullptr_t);
template <class T> bool operator==(nullptr_t, const shared_ptr<T> &);
template <class T> bool operator!=(nullptr_t, const shared_ptr<T> &);
These proposed shared_ptr methods seem to be multiplying at an
alarming rate. And as I pointed out in an earlier post, the
proliferation of these routines goes well beyond replacing 0's with
nullptr's - the kind of incremental improvement that nullptr provides
best. In fact, the wholesale addition of all of these these new,
nullptr_t-specific methods - might even discourage programmers from
adopting nullptr in their one code. Because one might easily (if
mistakenly) conclude that the use of nullptr requires duplicating
existing interfaces and specializing each method exclusively for a
nullptr argument - a cost in overhead that would exceed any perceived
benefit.
Fortunately, none of these additional shared_ptrs methods are needed -
as improbable as it might at first sound - there is in fact a way for
shared_ptr's existing interface to support nullptr arguments -
practically unchanged. In fact the set of changes that would need to
be made are so subtle as to be hardly noticeable:
// construction
template<class Y=T> explicit shared_ptr(Y* p);
template<class Y=T, class D> shared_ptr(Y* p, D d);
// comparison
template<class T, class U=T>
bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b);
template<class T, class U=T>
bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b);
template<class T, class U=T>
bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b);
// ... and so forth
As it turns out, the only change needed for shared_ptr to support
nullptr arguments - is to specify the type of a pointer template type
parameter - whenever that type cannot be deduced from the argument
provided.
Greg
---
[ 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://www.comeaucomputing.com/csc/faq.html ]