
On 23/05/2010 11:15, Neil Groves wrote:
I convinced myself that with static_casts do not break strict aliasing rules since the conversion from rv<T>* to T* is valid. I believe that section 5.2.9 para 2 applies:
The problem is not downcasting, but the fact that T is not rv<T> (the dynamic type of T is T, not rv<T>): "If the object of type “cv1 B” is actually a subobject of an object of type D, the result refers to the enclosing object of type D. Otherwise, the result of the cast is undefined." When we use static_cast<rv<T>>, the object T is not actually a subobject of rv<T>, so I think we're in undefined behaviour. Imagine this: class B { int filler; }; class D : public B { int filler2;} B b; D *dp = static_cast<D*>(&b); //dp is now pointing to garbage Best, Ion