title: Type matching in templates
body: <p>Playing with templates, I implemented this one for iterating arrays:</p>
  
  <pre><code>template&lt;class T, size_t size&gt;
  void iterate(T(&amp;arr)[size], void(*fn)(T&amp;)) {
    for(size_t i=0; i&lt;size; ++i) fn(arr[i]);
  }
  </code></pre>
  
  <p>So I could iterate arrays like this</p>
  
  <pre><code>void printn(int&amp; n) {
    printf("%d\n",n);
  }
  
  int a[] = {3,-4,6,2};
  iterate(a,printn);
  </code></pre>
  
  <p>Then I realized that I also need const iterator, so the first bad idea was to code</p>
  
  <pre><code>template&lt;class T, class U, size_t size&gt;
  void iterate(T(&amp;arr)[size], void(*fn)(U&amp;)) {
    for(size_t i=0; i&lt;size; ++i) fn(arr[i]);
  }
  </code></pre>
  
  <p>I know it is bad, but I wonder why this behavior:</p>
  
  <pre><code>void printn(int&amp; n); // works
  void printn(const int&amp; n); // also works
  void printn(short&amp; n); // type mismatch
  void printn(const short&amp; n); // works
  </code></pre>
  
  <p>There is also an issue with lambdas:</p>
  
  <pre><code>iterate(a,[](int&amp; n) { printf("%d\n",n); }); // type mismatch
  </code></pre>
  
  <p>So the question is <strong>Why the type mismatches and how to solve them?</strong></p>
  
  <p>I use 32bit MinGW 4.8.1 with <code>-std=C++11</code> option.</p>