mercredi 28 novembre 2018

static_cast dangling reference when downcasting

Is the following code correct or does it yield undefined behavior due to a dangling reference and why exactly is it correct:

class A {};
class B : public A {};

B& f(A& a) {
  // Dangling reference here?
  return static_cast<B&>(a);    
}

int main()
{
  A a;
  B& b = f(a);
}

Aucun commentaire:

Enregistrer un commentaire