title: c++11 local static member variable destruction order for shared_ptr objects
body: <p>I've been struggling with a destructor call order which I cannot really understand.</p>
  
  <p>Say we have the following definitions:</p>
  
  <pre><code>#include &lt;memory&gt;
  #include &lt;iostream&gt;
  
  class DummyClass {
      std::string name;
  public:
      DummyClass(std::string name) : name(name) { std::cout &lt;&lt; "DummyClass(" &lt;&lt; name &lt;&lt; ")" &lt;&lt; std::endl; }
      ~DummyClass() { std::cout &lt;&lt; "~DummyClass(" &lt;&lt; name &lt;&lt; ")" &lt;&lt; std::endl; }
  };
  
  
  class TestClass {
  private:
  
      static DummyClass dummy;
  
      static DummyClass&amp; objects() {
          static DummyClass dummy("inner");
          return dummy;
      }
  public:
      TestClass() {
          std::cout &lt;&lt; "TestClass" &lt;&lt; std::endl;
          std::cout &lt;&lt; "TestClass Objects is: " &lt;&lt; &amp;objects() &lt;&lt; std::endl;
      }
  
      virtual ~TestClass() {
          std::cout &lt;&lt; "~TestClass Objects is: " &lt;&lt; &amp;objects() &lt;&lt; std::endl;
          std::cout &lt;&lt; "~TestClass" &lt;&lt; std::endl;
      }
  };
  
  DummyClass TestClass::dummy("outer");
  </code></pre>
  
  <p>Now, If I instantiate the <code>TestClass</code> as follows:</p>
  
  <pre><code>TestClass *mTest = nullptr;
  
  int main() {
      mTest = new TestClass(); delete mTest;
      return 0;
  }
  </code></pre>
  
  <p>The output obtained is the one I would expect:</p>
  
  <pre><code>DummyClass(outer)
  TestClass
  DummyClass(inner)
  TestClass Objects is: 0x....
  ~TestClass Objects is: 0x....
  ~TestClass
  ~DummyClass(inner)
  ~DummyClass(outer)
  </code></pre>
  
  <p>But, now, if I use a shared_ptr for mTest, like:</p>
  
  <pre><code>std::shared_ptr&lt;TestClass&gt; mTest;
  
  int main() {
      mTest = std::make_shared&lt;TestClass&gt;();
      return 0;
  }
  </code></pre>
  
  <p>the output produced is:</p>
  
  <pre><code>DummyClass(outer)
  TestClass
  DummyClass(inner)
  TestClass Objects is: 0x....
  ~DummyClass(inner)
  ~TestClass Objects is: 0x....
  ~TestClass
  ~DummyClass(outer)
  </code></pre>
  
  <p>Can someone explain why is the DummyClass inner object being destroyed before the end of the TestClass object destructor, in this particular case?
  I found consistent behavior for gcc 5.2.0 using -std=gnu++11 and clang 3.8.0 with -std=c++11 but could not find any particular documentation citing this example.</p>
  
  <p><strong>Edit:</strong> To clarify: all of the code above was written in the same translation unit (*.cpp file) in the presented order. It is a simplification of a usage case where I have a header only class definition which must hold a static list of <code>this</code> pointers to derived class objects. These pointers are added via ctor and removed when the dtor is reached. The problem is triggered when destroying the last object. The list is kept inside a static method and accessed through it to achieve the <em>header only</em> goal.</p>