title: Deducing the template parameter of a templated class argument: const issue
body: <p>I think the problem is fairly common, so there should be a known solution. I came up with one, but I'm not really satisfied, so I'm asking here, hoping someone can help.</p>
  
  <p>Say I have a function, whose signature is</p>
  
  <pre><code>template&lt;typename T&gt;
  void foo(const MyArray&lt;const T&gt;&amp; x);
  </code></pre>
  
  <p>The const in the template parameter is to prevent me from changin the array content, since (for reasons beyond this question), the accessors (<code>[]</code> and <code>()</code>) of <code>MyArray&lt;T&gt;</code> are always marked const, and return references to <code>T</code> (hence, the const ensure safety, since <code>MyArray&lt;T&gt;::operator[]</code> returns <code>T&amp;</code>, while <code>MyArray&lt;const T&gt;::operator[]</code> returns <code>const T&amp;</code>).</p>
  
  <p>Great. However, templates with different template arguments are non related, so I can't bind a reference to <code>MyClass&lt;T&gt;</code> to a reference of <code>MyClass&lt;const T&gt;</code>, meaning I can't do this</p>
  
  <pre><code>MyArray&lt;double&gt; ar(/*blah*/);
  foo(ar);
  </code></pre>
  
  <p>Notice that, without a reference, the code above would work <em>provided</em> that there is a copy constructor that lets me create <code>MyArray&lt;const T&gt;</code> from <code>MyArray&lt;T&gt;</code>. However, I don't want to remove the reference, since the array construction would happen <em>a lot of times</em>, and, despite relatively cheap, its cost would add up.</p>
  
  <p>So the question: how can I call <code>foo</code> with an <code>MyArray&lt;T&gt;</code>?</p>
  
  <p>My only solution so far is the following:</p>
  
  <pre><code>MyArray&lt;T&gt; ar(/*blah*/);
  foo(reinterpret_cast&lt;MyArray&lt;const T&gt;&gt;(ar));
  </code></pre>
  
  <p>(actually in my code I hid the reinterpret cast in an inlined function with more verbose name, but the end game is the same). The class <code>MyArray</code> does not have a specialization for <code>const T</code> that makes it not reinterpretable, so the cast should be 'safe'. But this is not really a nice solution to read. An alternative, would be to duplicate <code>foo</code>'s signature, to have a version taking <code>MyArray&lt;T&gt;</code>, which implementation does the cast and calls the const version. The problem with this is code duplication (and I have quite a few functions <code>foo</code> that need to be duplicated).</p>
  
  <p>Perhaps some extra template magic on the signature of <code>foo</code>? The goal is to pass both <code>MyArray&lt;T&gt;</code> and <code>MyArray&lt;const T&gt;</code>, while still retaining const-correctness (i.e., make the compiler bark if I accidentally change the input in the function body).</p>
  
  <p><strong>Edit 1</strong>: The class <code>MyArray</code> (whose implementation is not under my control), has const accessors, since it stores pointers. So calling <code>v[3]</code> will modify the values in the array, but not the <em>members stored in the class</em> (namely a pointer and some smart-pointer-like metadata). In other words, the object is de facto not modified by accessors, though the array is. It's a semantic distinction. Not sure why they went this direction (I have an idea, but too long to explain).</p>
  
  <p><strong>Edit 2</strong>: I accepted one of the two answers (though they were somewhat similar). I am not sure (for reasons long to explain) that the wrapper class is doable in my case (maybe, I have to think about it). I am also puzzled by the fact that, while</p>
  
  <pre><code>template&lt;typename T&gt;
  void foo(const MyArray&lt;const T&gt;&amp; x);
  MyArray&lt;int&gt; a;
  foo(a);
  </code></pre>
  
  <p>does not compile, the following does</p>
  
  <pre><code>void foo(const MyArray&lt;const int&gt;&amp; x);
  MyArray&lt;int&gt; a;
  foo(a);
  </code></pre>
  
  <p>Note: <code>MyArray</code> does offer a templated "copy constructor" with signature</p>
  
  <pre><code>template&lt;typename S&gt;
  MyArray(const MyArray&lt;S&gt;&amp;);
  </code></pre>
  
  <p>so it can create <code>MyArray&lt;const T&gt;</code> from <code>MyArray&lt;T&gt;</code>. I am puzzled why it works when <code>T</code> is explicit, while it doesn't if <code>T</code> is a template parameter.</p>