title: What are the rules around instantiation of template types (class/function) when taking an address?
body: <p>In answering <a href="https://stackoverflow.com/questions/6734492/c-callback-to-function-template-explicitly-instantiate-template">this</a> question, I came across this difference in behaviour with respect to template instantiation.</p>
  
  <p>Initially there is a function template</p>
  
  <pre><code>template &lt;typename T&gt; void my_callback(void* data) { … }
  </code></pre>
  
  <p>Now something requires the address of this - specifically a <code>void*</code>, so the obvious approach is</p>
  
  <pre><code>bar(reinterpret_cast&lt;void*&gt;(&amp;my_callback&lt;int&gt;));
  </code></pre>
  
  <p>However, with compiler versions pre gcc 4.5, this fails with a not-enough context... error. Fine - so the fix is to "cast" first - which forces instantiation, i.e:</p>
  
  <pre><code>void (*callback)(void*) = my_callback&lt;int&gt;;
  bar(reinterpret_cast&lt;void*&gt;(callback));
  </code></pre>
  
  <p>This works fine. </p>
  
  <p>Now the second scenario, rather than being a free function, it's a static member of a class template, i.e.</p>
  
  <pre><code>template &lt;typename T&gt;
  struct foo
  {
    static void my_callback(void* data) {
      T&amp; x = *static_cast&lt;T*&gt;(data);
      std:: cout &lt;&lt; "Call[T] with " &lt;&lt; x &lt;&lt; std::endl;
    }
  };
  </code></pre>
  
  <p>Now, the original <code>reinterpret_cast</code> works fine.</p>
  
  <pre><code>bar(reinterpret_cast&lt;void*&gt;(&amp;foo&lt;int&gt;::my_callback));
  </code></pre>
  
  <p>So my question is - why this apparent difference in behaviour?</p>