title: Generating code (at compile-time) to call a static function of every instantiation of a template
body: <p>My problem could be described with the example below.</p>
  
  <p>I want to call a static function from each instantiation of a template. What I am thinking of is something like a tuple which gets expanded each time a new instantiation of a template is encountered, so that I can access that tuple recursively and call a static function of each of the types in the tuple.</p>
  
  <pre><code>#include &lt;vector&gt;
  
  template&lt;typename T&gt;
      struct Junk
      {
          static std::vector&lt;T&gt; all;
          Junk(T t)
          {
              all.push_back(t);
          }
  
          static void clear()
          {
              all.clear();
          }
      };
  
  template&lt;typename T&gt;
      std::vector&lt;T&gt; Junk&lt;T&gt;::all;
  
  void clearJunk()
  {
      Junk&lt;float&gt;::clear();
      Junk&lt;char&gt;::clear();
      Junk&lt;unsigned int&gt;::clear();        
      // how can I generalize this?
  }
  
  int main()
  {
      // instantiate Junk with different types
      Junk&lt;float&gt; f1(1.0f);
      Junk&lt;float&gt; f2(2.0f);
      // Junk&lt;float&gt;::all.size() == 2
  
      Junk&lt;char&gt; c1('a');
      Junk&lt;char&gt; c2('b');
      Junk&lt;char&gt; c3('c');
      // Junk&lt;char&gt;::all.size() == 3
  
      Junk&lt;unsigned int&gt; i1(1);
      // Junk&lt;unsigned int&gt;::all.size() == 1
  
      // clear all Junk
      clearJunk();
  
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>A run-time solution to this would be a vector of function pointers, and the first object of each template instantiation pushes a pointer to the static member function to that vector:</p>
  
  <pre><code>std::vector&lt;void(*)()&gt; clear_funcs;
  
  template&lt;typename T&gt;
      struct Junk
      {
          static std::vector&lt;T&gt; all;
          Junk(T t)
          {
              (void)init;   // mention init at least once so its constructor will be invoked
              all.push_back(t);
          }
  
          static void clear()
          {
              all.clear();
          }
          struct At_Init
          {
              At_Init()
              {
                  clear_funcs.push_back(&amp;Junk&lt;T&gt;::clear);
              }
          };
          static At_Init init; // will only be constructed once for each template instantiation
      };
  
  template&lt;typename T&gt;
      std::vector&lt;T&gt; Junk&lt;T&gt;::all;
  
  template&lt;typename T&gt;
      typename Junk&lt;T&gt;::At_Init Junk&lt;T&gt;::init;
  
  void clearJunk()
  {
      // call every clear() of all the Junk template instantiations
      for (void(*clear)() : clear_funcs) {
          clear();
      }
  }
  </code></pre>
  
  <p>But this solution is not as fast as a compile time solution would be and it requires a lot of code in the class that is not at all meaningful, so I dislike this solution, although it works.</p>
  
  <p>How could you generate code at compile-time to call a static function of every instantiation of a template?</p>