Does the following reinterpret_cast lead to undefined behavior?

Viewed 241

Does the reinterpret_cast in the code below lead to undefined behavior? In case it does, is it possible to define rpd in a type-safe manner?

class Base
{ 
public:
  virtual ~Base() = default;
};

class Derived : public Base { };

int main(void)
{
  Derived d;
  Base* pb = &d;
  Base*& rpb = pb;

  Derived*& rpd = reinterpret_cast<Derived*&>(rpb);

  return 0;
}

Sort of related to my previous recent question. Context behind this; I am experimenting with an adapter class that should allow vectors containing covariant pointer types to be used as covariant types themselves.

1 Answers
Related