class T
{};
class UseT
{
public:
//...
boost::shared_ptr<const T> getT() const
{
return m_t;
}
private:
boost::shared_ptr<T> m_t;
};
Question> What are the rules used when we convert from boost::shared_ptr<T> to boost::shared_ptr<const T>?
shared_ptr<T>has a converting constructor that allows it to be constructed fromshared_ptr<U>if it would be valid to convert fromU*toT*, mirroring how built-in pointers work.(For
std::shared_ptrthe constructor can only be called ifU*is convertible toT*, but forboost::shared_ptrI’m not sure if it checks that, or you just get a compiler error for invalid conversions.)Since
T*can be converted toconst T*, the constructor allows you to create ashared_ptr<const T>from ashared_ptr<T>.