title: Undefined reference to static const int
body: <p>I ran into an interesting issue today.  Consider this simple example:</p>
  
  <pre><code>template &lt;typename T&gt;
  void foo(const T &amp; a) { /* code */ }
  
  // This would also fail
  // void foo(const int &amp; a) { /* code */ }
  
  class Bar
  {
  public:
     static const int kConst = 1;
     void func()
     {
        foo(kConst);           // This is the important line
     }
  };
  
  int main()
  {
     Bar b;
     b.func();
  }
  </code></pre>
  
  <p>When compiling I get an error:</p>
  
  <pre><code>Undefined reference to 'Bar::kConst'
  </code></pre>
  
  <p>Now, I'm pretty sure that this is because the <code>static const int</code> is not defined anywhere, which is intentional because according to my understanding the compiler should be able to make the replacement at compile-time and not need a definition.  However, since the function takes a <code>const int &amp;</code> parameter, it seems to be not making the substitution, and instead preferring a reference.  I can resolve this issue by making the following change:</p>
  
  <pre><code>foo(static_cast&lt;int&gt;(kConst));
  </code></pre>
  
  <p>I believe this is now forcing the compiler to make a temporary int, and then pass a reference to that, which it can successfully do at compile time.</p>
  
  <p>I was wondering if this was intentional, or am I expecting too much from gcc to be able to handle this case?  Or is this something I shouldn't be doing for some reason?</p>