title: Is using std::vector< std::shared_ptr<const T> > an antipattern?
body: <p>For a long time I was using <code>std::vector</code> and <code>std::shared_ptr</code> hand in hand. Recently I started using <code>std::shared_ptr&lt;const T&gt;</code> whenever a pointer to a const object was needed. This is all OK, since <code>std::shared_ptr&lt;T&gt;</code> can be cast to <code>std::shared_ptr&lt;const T&gt;</code> and then they share the same reference counter and everything feels natural.</p>
  
  <p>But when I try to use constructs such as <code>std::vector&lt; std::shared_ptr&lt;const T&gt; &gt;</code> I run into troubles. To simplify I will denote the two structures:</p>
  
  <pre><code>template &lt;class T&gt;
  using SharedPtrVector = std::vector&lt; std::shared_ptr&lt;T&gt; &gt;;
  
  template &lt;class T&gt;
  using SharedConstPtrVector = std::vector&lt; std::shared_ptr&lt;const T&gt; &gt;;
  </code></pre>
  
  <p>The problem is that although <code>SharedPtrVector</code> and <code>SharedConstPtrVector</code> are very similar, <code>SharedConstPtrVector</code> cannot be cast to <code>SharedPtrVector</code>.</p>
  
  <p>So each time I want to be a const correct and write a function such as:</p>
  
  <pre><code>void f(const SharedConstPtrVector&lt;T&gt;&amp; vec);
  </code></pre>
  
  <p>there is no way I can pass const <code>SharedPtrVector&lt;T&gt;</code> to <code>f</code>.</p>
  
  <p>I was thinking about this a lot and considered several alternatives:</p>
  
  <ol>
  <li><p>Write conversion functions</p>
  
  <pre><code>template &lt;typename T&gt;
  SharedConstPtrVector&lt;T&gt; toConst(const SharedPtrVector&lt;T&gt;&amp;);
  </code></pre></li>
  <li><p>Write code in generic form:</p>
  
  <pre><code>template &lt;typename T&gt;
  void f(const std::vector&lt; std::shared_ptr&lt;T&gt; &gt;&amp; vec);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>template &lt;typename TIterator&gt;
  void f(TIterator begin, TIterator end);
  </code></pre></li>
  <li><p>Abandon the idea of <code>std::vector&lt; std::shared_ptr&lt;const T&gt; &gt;</code></p></li>
  </ol>
  
  <p>The problem with 1. is the computational overhead and increased uglyness of code, while 2. gives the code an "everything is a template" flavor.</p>
  
  <p>I am an inexperienced programmer and I don't want to set out in the wrong direction. I would like to hear advice from someone who has experience with this problem.</p>