Re: Undefined behaviour with Non-static, non-polymorphic + null
pointer?
On Nov 21, 6:42 pm, werasm <wer...@gmail.com> wrote:
On Nov 21, 4:29 pm, Alan Woodland <aj...@aber.ac.uk> wrote:
The Standard says that "p->" is
converted to "(*p)." (see section 5.2.5) and no matter how you slice it,=
*p is a dereference. Dereferencing a null pointer results in undefined
behaviour.
Yes, but doing this:
sizeof( static_cast<P*>(0)->member ); //or
sizeof( *static_cast<P*>(0)->member )
would not invoke cause behavior (for interest sake) as
this dereference is "sliced" at compile time.
There's no slicing involved, but the standard explicitly says
that the arguments of sizeof are not evaluated, so no runtime
undefined behavior can result.
Note that the fact that they are not evaluated has other
implications as well. For example, if you write "sizeof(f())",
you're not required to provide an implementation of f. And if
you write "sizeof(f<int>())", the template function f is not
instantiated for int.
--
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