Re: [Defect Report] shared_ptr and nullptr
On 11/5/07 9:52 AM, in article 74RWi.162147$U01.1154008@twister1.libero.it,
"Alberto Ganesh Barbati" <AlbertoBarbati@libero.it> wrote:
Greg Herlihy ha scritto:
I think it is a good idea to replace 0 with nullptr wherever appropriate. I
think that nullptr works best as a "universally-compatible" null pointer
constant. The problem with the proposed nullptr_t overloads in shared_ptr's
interface - is that nullptr_t is not appearing in the context of any
specific pointer type, but has instead become a type unto itself. Yet
shared_ptr never cared to support initialization with the old "0" null
pointer constant (and still would not support "0" even with these changes).
So why should shared_ptr now implement an entire set of routines - just
because "0" has been renamed?
Because it has not just been renamed. nullptr is not just a special name
for 0, the former is a pointer (with an unspecified pointee type), while
the latter is an integer (that happens to be convertible to a pointer
type in some contexts).
No, "0" in C++ really does express two distinct concepts (an integer
constant and a null pointer constant). Now, whether "0" expresses both
concepts at once (the "0 duality" school of thought) or expresses either one
concept or the other - depending on its context (the "dual 0s" line of
reasoning) is a question that has long been debated. Fortunately, the
nullptr proposal itself authoritatively answers this question (and thereby
shuts the door on any future debate):
"0 is always both an integer constant and a null pointer constant, except
when it's not."
The question is if
shared_ptr<A> ptr(nullptr);
should be equivalent to
shared_ptr<A> ptr((A*)0); // #1
or rather to
shared_ptr<A> ptr; // #2
I think initializing a shared_ptr with a nullptr value should be equivalent
to this code:
A* p = nullptr;
shared_ptr<A> ptr(p);
Since only #1 would call the same constructor as the above code, #1 is the
only reasonable interpretation of initializing a shared_ptr with a nullptr
argument.
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 ]