title: How to use boost::atomic_store with shared_ptr<T> and shared_ptr<const T>?
body: <p>In my case <code>T</code> is <code>pcl::PointCloud&lt;pcl::PointXYZ&gt;&gt;</code> but the question shoud stand for any type <code>T</code>. The following example produces an error:</p>
  
  <pre><code>using pc = pcl::PointCloud&lt;pcl::PointXYZ&gt; &gt;;
  boost::shared_ptr&lt;pc&gt; p(new pc);
  boost::shared_ptr&lt;const pc&gt; const_p(new pc);
  
  // This is legal
  const_p = p;
  
  // The atomic equivalent is not
  boost::atomic_store(&amp;const_p, p);
  </code></pre>
  
  <p>The problem is that the <code>boost::atomic_store</code> expects both arguments to be <code>T*</code> and <code>T</code>, but these are considered different types despite the fact that it's perfectly safe to assign <code>p</code> to <code>const_p</code>. The following doesn't work either.</p>
  
  <pre><code>boost::atomic_store(&amp;const_p, const_cast&lt;boost::shared_ptr&lt;const pc&gt; &gt; (p));
  </code></pre>
  
  <p>Despite the above basically casting a <code>pc*</code> to <code>const pc*</code> which is perfectly safe, it produces an error about <code>const_cast</code> not being able to convert to different type. I understand that because <code>pc</code> is a template argument, it is considered part of the type of the <code>shared_ptr</code> and not a cv qualification. The following work</p>
  
  <pre><code>boost::atomic_store(&amp;const_p, boost::shared_ptr&lt;const pc&gt;(p));
  </code></pre>
  
  <p>However, it creates an extra unecessary <code>boost::shared_ptr</code>. It is my understanding that the same is true for <code>boost::const_pointer_cast&lt;const pc&gt;(p)</code> This can be avoided if <code>p</code> is no longer needed.</p>
  
  <pre><code>boost::atomic_store(&amp;const_p, boost::shared_ptr&lt;const pc&gt;(std::move(p));
  </code></pre>
  
  <p>This still creates an extra object but it shouldn't matter because the reference count is not modified, which is the expensive part of copying a <code>shared_ptr</code> on account of being atomic.</p>
  
  <p>It just so happens that this occurs in a non-critical part of my code so I'm fine with the above, but I would like to know for future reference: If <code>std::move</code> was not an option, how would one atomically store a <code>boost::shared_ptr&lt;T&gt;</code> to a <code>boost::shared_ptr&lt;const T&gt;</code> without the overhead of creating an unecessary temporary pointer? It should be possible because it is safe to view a <code>T</code> through a <code>const T*</code>, but I can't figure out a way to do it.</p>