title: C++ struct declared in function visible in main
body: <p>why does this code work? with <code>c++14</code></p>
  
  <pre><code>// Example program
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  
  auto fun()
  {
      struct a
      {
          int num = 10;
          a()
          {
  
              cout &lt;&lt; "a made\n";
          }
          ~a()
          {
              cout &lt;&lt; "a destroyed\n";
          }
      };
      static a a_obj;
      return a_obj;
  }
  
  
  int main()
  {
      auto x = fun();
      cout &lt;&lt; x.num &lt;&lt; endl;
  
  }
  </code></pre>
  
  <p>how is the type <code>a</code> visible in main? if i change <code>auto x=</code> to <code>a x=</code> it obviously doesn't compile, but how does main know about the type <code>a</code>? </p>
  
  <p>The <code>static</code> declaration is there since I was trying to test for something else but then I stumbled upon this behavior. </p>
  
  <p>Running it here: <a href="https://wandbox.org/permlink/rEZipLVpcZt7zm4j" rel="noreferrer">https://wandbox.org/permlink/rEZipLVpcZt7zm4j</a></p>