Search code examples
c++pointersshared-ptr

shared_ptr error expression must have arithmetic, enum, pointer


I'm trying to check if a shared_ptr didn't call shared_ptr.reset()

I have a

std::shared_ptr<Shape> m_shape; 

and I'm trying to do

 if(m_shape.reset()==false)
 {
    dothis();
 }

i want to check and see if m_shape is active and being used... and reset says when a shared_ptr stopped being used

but i keep getting an error on m_shape saying that expression must have arithmetic, enum, pointer


Solution

  • reset() is a function that resets the shared_ptr, and returns void. It most certainly does not tell you if the shared_ptr is currently managing an object. In fact, calling it guarantees that it's no longer managing an object.

    Instead, shared_ptr has a conversion to bool that tells you if it's currently managing an object. So you can just say

    if (m_shape) {
        // m_shape has an object
    }