Boost logo

Boost :

From: Martin Bonner (Martin.Bonner_at_[hidden])
Date: 2007-10-11 16:48:38


From: Fernando Cacciola

> Anyway, let's not digress. This started becasue you mentioned that you used
> reinterpret_cast<>, but your are using static_cast<> instead.
> As long as you don't step over "void*" in the way fom one type to another,
> static_cast<> is perfectly safe.
>
> If you were to screw up the derived class in such a way that it prevents the
> cast to be 100% safe, the compiler will just reject your code.
> That's why static_cast<> was invented for.

I don't think that's true. Given:
     class Base {....}
     struct Derived : public Base{ int DerivedFunction(); };
 
     Base b;
     Derived* pDerived = static_cast<Derived*>(&b);
 
     pDerived->DerivedFunction(); // Undefined behaviour.
The behaviour of the call to DerivedFunction is not defined by the standard. (Of course, on many compilers today it will do what the author expected.)
 
What is more, this is exactly where type aliasing analysis comes in. The compiler knows that b is of type Base, so it knows that it can ignore any changes to memory done through a pointer to Derived when it is optimising.




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk