CWG2861 [expr.dynamic.cast] The behaivor is underspecified when v does not point to that object (original) (raw)

Full name of submitter (unless configured in github; will be published with the issue): Jim X

Consider this example:

struct A{ virtual void foo(){} }; struct B{ virtual void foo2(){} }; struct D:A,B{ virtual void foo(){

} }; struct No{}; int main(){ No no; B* ptr = (B*)&no; try{ auto r = dynamic_cast<D*>(ptr); std::cout<< (long long int)r; }catch(...){

}; }

This example just causes the segment fault in major implementations. However, [expr.dynamic.cast] does not specify the behavior for this case.

Suggested Resolution

Append a wording before [expr.dynamic.cast] p7

If the type of the object pointed or referred to by v is not similar to the pointed or referred type by v, the behavior is undefined.