C++ chaining references with locals in between

I have multiple references referencing other references, but some of these references in between are contained in an object which goes out of scope (or is destructed) before the final object referenced to.

That is, the object being referenced has a longer lifetime than the references in between. Now, is accessing the first reference undefined behaviour?

It is hard to explain, but the following code should be clearer:

#include <iostream>

static const int x = 10;

struct Q
{
const int& q = x;
};

void foo(int& v)
{
Q q;
v = q.q;
} // q goes out of scope

int main()
{
int n = 4;
foo(n);
std::cout << n;
}

It seems to work fine for me and clang and gcc do not give me warnings either.