Re: Dereferencing a null-pointer allowed?
On May 10, 7:26 pm, Greg Herlihy <gre...@pacbell.net> wrote:
On May 10, 6:06 am, Ron Natalie <r...@spamcop.net> wrote:
James Kanze wrote:
With one important exception, I believe: if the expression is
used as an argument to sizeof or typeid. Something like:
sizeof( ((T*)0)->f() )
is legal, I think.
Correct, sizeof doesn't evaluate it's argument, it merely
checks the type.
Typeid does however potentially do so, since the result
may be something it has to test the dynamic typing of.
Nonetheless, dereferencing a null pointer in a typeid expression has
well-defined behavior - the result is a thrown bad_typeid exception,
per ?5.2.8/2.
No. The only case that happens is if the argument to typeid is
an lvalue expression which "is obtained by applying * to a
pointer". In an example such at the above (replacing sizeof
with typeid), if the return type of T::f() is anything but a
reference to a polymorphique type, the expression is not
evaluated, at all. If the return type is a reference to a
polymorphic object, then the expression is evaluated, and we get
undefined behavior (probably a core dump if T::f() is virtual,
or uses the contents of the class in any way).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
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
---
[ 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 ]