Re: Null references.
* James Kanze:
On Jul 16, 5:04 am, "Alf P. Steinbach" <al...@start.no> wrote:
There are some exceptions to incurring UB for dereferencing a
nullpointer. The most important one is that any given compiler
vendor can decide to give that a well-defined meaning at
compile time for that compiler, and exploit that to implement
various macros in the standard library. Another one is the
context of a typeid call.
Hows that?
Modulo vendor specific guarantees (of course), it's undefined
behavior to dereference a null pointer in code that actually
gets executed. Something like sizeof( *p ) is fine, even if p
is a null pointer. But typeid normally requires execution,
e.g.: typeid( *p ) is undefined behavior if p is a null pointer
(and the pointed to object has a polymorphic type, but then,
that's the usual reason for using typeid).
I'm sorry, that's incorrect. ?5.2.8/2, typeid guarantees to throw a
std::bad_typeid exception if you give it an lvalue formed by dereferencing a
nullpointer to polymorphic type. So we're out of UB-land and into guaranteed
exception-land. :-)
Cheers,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?