In my case T
is pcl::PointCloud<pcl::PointXYZ>>
but the question shoud stand for any type T
. The following example produces an error:
using pc = pcl::PointCloud<pcl::PointXYZ> >;
boost::shared_ptr<pc> p(new pc);
boost::shared_ptr<const pc> const_p(new pc);
// This is legal
const_p = p;
// The atomic equivalent is not
boost::atomic_store(&const_p, p);
The problem is that the boost::atomic_store
expects both arguments to be T*
and T
, but these are considered different types despite the fact that it's perfectly safe to assign p
to const_p
. The following doesn't work either.
boost::atomic_store(&const_p, const_cast<boost::shared_ptr<const pc> > (p));
Despite the above basically casting a pc*
to const pc*
which is perfectly safe, it produces an error about const_cast
not being able to convert to different type. I understand that because pc
is a template argument, it is considered part of the type of the shared_ptr
and not a cv qualification. The following work
boost::atomic_store(&const_p, boost::shared_ptr<const pc>(p));
However, it creates an extra unecessary boost::shared_ptr
. It is my understanding that the same is true for boost::const_pointer_cast<const pc>(p)
This can be avoided if p
is no longer needed.
boost::atomic_store(&const_p, boost::shared_ptr<const pc>(std::move(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 shared_ptr
on account of being atomic.
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 std::move
was not an option, how would one atomically store a boost::shared_ptr<T>
to a boost::shared_ptr<const T>
without the overhead of creating an unecessary temporary pointer? It should be possible because it is safe to view a T
through a const T*
, but I can't figure out a way to do it.