Convert from boost :: shared_ptr <T> to boost :: shared_ptr <const T>

class T
{};

class UseT
{
public:
    //...
    boost::shared_ptr<const T> getT() const
    {
        return m_t;
    }
private:
    boost::shared_ptr<T> m_t;
};

      

Questions> What are the rules used when converting from boost::shared_ptr<T>

to boost::shared_ptr<const T>

?

+3


source to share


1 answer


shared_ptr<T>

has a conversion constructor that lets you construct it from shared_ptr<U>

if it is valid to convert from U*

to T*

, reflecting how inline pointers work.

template<typename U>
  shared_ptr(const shared_ptr<U>& other);

      



(For, the std::shared_ptr

constructor can only be called if it U*

converts to T*

, but for boost::shared_ptr

I'm not sure if it checks this, or if you're just getting a compiler error for invalid conversions.)

Since T*

you can convert to const T*

, the constructor lets you create shared_ptr<const T>

from shared_ptr<T>

.

+1


source







All Articles