title: Static template data members storage
body: <p>First I'll write example to properly address the question.</p>
  
  <p>First of all, I'll declare template to be used to create singleton object (not auto-created):
  <b>singleton_base.h</b></p>
  
  <pre><code>template &lt;class Derived&gt;
  class SingletonBase
  {
    public:
      static Derived* instance() { assert(s_instance); return dynamic_cast&lt;Derived*&gt;(s_instance); }
    protected:
      SingletonBase() { assert(s_instance==0); s_instance=this; }
      virtual ~SingletonBase() { assert(s_instance); s_instance=0; }
    private:
      static SingletonBase* s_instance;
  };
  
  template &lt;class Derived&gt;
  SingletonBase&lt;Derived&gt;* SingletonBase&lt;Derived&gt;::s_instance = 0;
  </code></pre>
  
  <p>Now we can declare any class that derived from template, and each derived class should have its own s_instance. For example:</p>
  
  <p><b>child1.h</b></p>
  
  <pre><code>class Child1 : public SingletonBase&lt;Child1&gt;
  {
    ...
    void doSomething();
    static void staticInvokeOne();
  };
  </code></pre>
  
  <p><b>child2.h</b></p>
  
  <pre><code>class Child2 : public SingletonBase&lt;Child2&gt;
  {
    ...
    void doSomethingElse();
    static void staticInvokeBoth();
  };
  </code></pre>
  
  <p>I also have Child's implementation in <b>child1.cpp</b> and <b>child2.cpp</b> respectively.</p>
  
  <p><b>child1.cpp</b></p>
  
  <pre><code>void Child1::staticInvokeOne()
  {
    instance()-&gt;doSomething();
  }
  </code></pre>
  
  <p><b>child2.cpp</b></p>
  
  <pre><code>void Child2::staticInvokeBoth()
  {
    Child1::instance()-&gt;doSomething();
    instance()-&gt;doSomethingElse();
  }
  </code></pre>
  
  <p>Now I have <code>Child1</code> and <code>Child2</code> having their own <code>s_instance</code> and they will point to the only instance of that class at given moment.</p>
  
  <p>The question is about storage for this static data member <code>s_instance</code>. Unlike with regular static data members I have not specified where it should be allocated. I would, of course, like to have <code>SingletonBase&lt;Child1&gt;::s_instance</code> and <code>SingletonBase&lt;Child2&gt;::s_instance</code> in <b>child1.o</b> and <b>child2.o</b> respectively, but is that something I can expect or enforce?</p>
  
  <p>The issue gets more complicated if I put <code>Child1</code> and <code>Child2</code> into two distinct libraries - <b>lib1</b> and <b>lib2</b>. Inside <code>Child2::staticInvokeBoth()</code> there is a call to <code>Child1::instance()</code>. As far as I understand, default gcc's behaviour is to generate a copy of <code>SingletonBase&lt;Child1&gt;::s_instance</code> in each compilation unit, thus one copy will be emitted in <b>lib2</b>. </p>
  
  <p>Will it also generate a copy of <code>SingletonBase&lt;Child1&gt;::s_instance</code> in <b>lib2</b>? Definitely, one copy of <code>SingletonBase&lt;Child1&gt;::s_instance</code> should be in <b>lib1</b>. If those two libraries are later used together in one application, can I be sure that there is only one instance of <code>SingletonBase&lt;Child1&gt;::s_instance</code> and that both <code>Child1::staticInvokeOne()</code> and <code>Child2::staticInvokeBoth()</code> are using it?</p>
  
  <p>Is it generally safe to use this approach with statics wrapped in template, or are there any drawbacks?</p>
  
  <p>Thank you in advance!</p>