On Sep 15, 1:21 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> puzzlecracker wrote:
[...]
> > Well, if we insert a null pointer into a set, wouldn't it
> > cause an error whenever the container would try to invoke
> > its member.
> Yes. Dereferencing a null-pointer is undefined behavior. That,
> however, does not imply that you cannot insert the
> null-pointer into a set. You just have to make sure that it
> does not get dereferenced.
Also, std::set<T*> never dereferences the pointers it contains.
Let's face it, this is pure template code, with no partial
specializations or whatever, so the code is exactly the same for
pointers and for non-pointers.
Of course, if you explicitly specialize std::less< A* > so that
it does dereference the pointer, you'll get into deep trouble if
the pointer is null. But that would be just stupid (unless your
goal is obfuscation). The standard defines a full ordering on
pointers (using std::less, but not with <), and requires
std::set to use this ordering by default. A reader who sees a
set instantiated without a custom ordering function has a right
to expect that this ordering is what is used, and not something
else. (If this ordering isn't appropriate, and it often isn't,
then you should says so explicitly, by providing an ordering
argument to the template instantiation.)
--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
|