Skip to content

CWG2861 [expr.dynamic.cast] The behaivor is underspecified when v does not point to that object #497

Closed
cplusplus/draft
#7099
@xmh0511

Description

@xmh0511

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions