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);
}

```