title: Template container with multiple template parameters interacting with other template containers with a different template parameter
body: <p>Wordy title, yes, but I was unsure how else to say it.  Suppose I have a container class which has two template parameters, the first of which is a type, the second of which is the size of the local storage for the container.  </p>
  
  <p>Now we have multiple containers with a different container storage size.  Essentially, the container functions (all the public ones, anyway) only really care about <code>T</code>; <code>N</code> is only used to allocate local storage (an allocator is used if <code>N</code> is not enough).</p>
  
  <p>I have put together a simple example implementation that showcases the problem I am having.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  template &lt;typename T, size_t N = 10&gt;
  class TestArray
  {
  public:
      T Local[N];
  
      class Iterator
      {
      public:
          T* Array;
          int Index;      
  
          Iterator() : Array(NULL), Index(-1) { }
          Iterator(T* _array, int _index) : Array(_array), Index(_index) { }
  
          bool operator == (const Iterator&amp; _other) const 
          { 
               return _other.Index == Index &amp;&amp; _other.Array == Array; 
          }
  
          void Next() { ++Index; }
          void Prev() { --Index; }
  
          T&amp; Get() { return Array[Index]; }
      };
  
      T&amp; operator [] (const int _index) { return Local[_index]; }
  
      Iterator Begin() { return Iterator(Local, 0); }
      Iterator End() { return Iterator(Local, N); }
  
      template &lt;size_t _N&gt;
      void Copy(const TestArray&lt;T, _N&gt; &amp;_other, int _index, int _count)
      {   
          int i;
  
          for (i = 0; i &lt; _count; i++)
              Local[_index + i] = _other[i];
      }   
  };
  </code></pre>
  
  <p>This is really a two part question.  I will concern this question only with the first part, and ask another regarding the second.  I tried using it as follows:</p>
  
  <pre><code>int main() {
  
      TestArray&lt;int&gt; testArray1;
      TestArray&lt;int, 25&gt; testArray2;
  
      TestArray&lt;int&gt;::Iterator itr1;
      TestArray&lt;int, 25&gt;::Iterator itr2;
  
      itr1 = testArray1.Begin();
  
      for (itr1 = testArray1.Begin(); itr1 != testArray1.End(); itr1.Next())
      {
          itr1.Get() = itr1.Index;
      }
  
      testArray2.Copy(testArray1, 0, 10);
  
      for (itr2 = testArray2.Begin(); itr2 != testArray2.End(); itr2.Next())
      {
          std::cout &lt;&lt; itr2.Get() &lt;&lt; std::endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Here is an IDEONE link: <a href="http://ideone.com/1XKwD" rel="nofollow">http://ideone.com/1XKwD</a></p>
  
  <p>When compiled with gcc-4.3.4, I get the following.</p>
  
  <pre><code>prog.cpp: In member function ‘void TestArray&lt;T, N&gt;::Copy(const TestArray&lt;T, _N&gt;&amp;, int, int) [with unsigned int _N = 10u, T = int, unsigned int N = 25u]’:
  prog.cpp:82:   instantiated from here
  prog.cpp:63: error: passing ‘const TestArray&lt;int, 10u&gt;’ as ‘this’ argument of ‘T&amp; TestArray&lt;T, N&gt;::operator[](int) [with T = int, unsigned int N = 10u]’ discards qualifiers
  </code></pre>
  
  <p>When compiled with VS2010, I get the following.</p>
  
  <pre><code>1&gt;------ Build started: Project: testunholytemplatemess, Configuration: Debug Win32 ------
  1&gt;  main.cpp
  1&gt;c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(63): error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const TestArray&lt;T&gt;' (or there is no acceptable conversion)
  1&gt;          with
  1&gt;          [
  1&gt;              T=int
  1&gt;          ]
  1&gt;          c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(44): could be 'int &amp;TestArray&lt;T&gt;::operator [](const int)'
  1&gt;          with
  1&gt;          [
  1&gt;              T=int
  1&gt;          ]
  1&gt;          while trying to match the argument list '(const TestArray&lt;T&gt;, int)'
  1&gt;          with
  1&gt;          [
  1&gt;              T=int
  1&gt;          ]
  1&gt;          c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(82) : see reference to function template instantiation 'void TestArray&lt;T,N&gt;::Copy&lt;10&gt;(const TestArray&lt;T&gt; &amp;,int,int)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              T=int,
  1&gt;              N=25
  1&gt;          ]
  </code></pre>
  
  <p>Maybe I'm being thick, but I'm failing to interpret what either of these is actually trying to tell me (still somewhat new to templates).  I also fail to understand why the <code>operator []</code> method should really care about <code>N</code>, or the fact that I'm calling <code>operator []</code> on a container with a different <code>N</code> value.  If you change <code>_other[i]</code> to <code>_other.Local[i]</code>, it works fine.</p>
  
  <p>Does anyone have any suggestions?</p>