Re: Null references.
On Jul 16, 1:28 pm, "Alf P. Steinbach" <al...@start.no> wrote:
* 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. =A75.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. :-)
Interesting. A bit strange, but who's to complain. Anything
which reduces undefined behavior is good.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34