How does the compiler resolve infinite reference loops?
<pre><code>// edited by Neil Butterworth to conserve vertical space
#include &lt;stdio.h&gt;

struct A;
struct B;

A&amp; GetAInstance();
B&amp; GetBInstance();

struct A {  
    A() {
        printf( "A\n" );
    }
    ~A() {
        printf( "~A\n" );
        B&amp; b = GetBInstance();
    }
};

struct B {
    B() {
        printf( "B\n" );
    }

    ~B() {
        printf( "~B\n" );
        A&amp; a = GetAInstance();
    }
}; 

A&amp; GetAInstance() {
    static A a;
    return a;
}

B&amp; GetBInstance() {
    static B b;
    return b;
}

int main( ) {
    A a;
}
</code></pre>

<p>Consider the above scenario. I would expect this to result in an infinite reference loop resulting in the program being unable to exit from static de-initialization, but the program ran just fine with the following printout:</p>

<pre><code>  A
  ~A
  B
  ~B
  A
  ~A
</code></pre>

<p>Which was unexpected.</p>

<p>How does a compiler deal with this situation? What algorithms does it use to resolve the infinite recursion? Or have I misunderstood something fundamental? Is this, somewhere in the standard, defined as undefined?</p>
