Search code examples
c++gccvectorshared-ptrweak-ptr

No match for operator== (weak_ptr, const weak_ptr)


I have the following data structure:

shared_ptr<vector<shared_ptr<Drawable>>> foo;

And a Renderer class with a function:

void addObject(weak_ptr<T> _obj) const

This function simply pushes back _obj into

mutable vector<weak_ptr<T>> m_objects;

When I try the following:

Renderer r;
for(auto& d: *foo) {
    r.addObject(d);
}

I get the following error with GCC 5.1:

error: no match for 'operator==' (operand types are 'std::weak_ptr<Drawable>' and 'const std::weak_ptr<Drawable>')|

I don't understand where the const is coming from.

foo 

isn't const in any way, and addObject doesn't take a const weak_ptr.

EDIT: I think I was too minimal. Here's the contents of addObject:

void addObject(std::weak_ptr<T> _obj) const {
        auto it = std::find(m_objects.begin(), m_objects.end(), _obj);

        if(it == m_objects.end()) {
            m_objects.push_back(_obj);
        }
    };

It works if I comment out everything but the actual push_back line. I'm guessing the iterator is assigning itself as an iterator to const weak_ptr. I just want to avoid adding it to the vector if it already exists.


Solution

  • weak_ptr itself has no operator ==, so you cannot find it in vector. You should cast it to shared_ptr and only then compare. So you should use lock function on each object. Or compare not pointers, but objects by some criteria.

    Like that

    #include <memory>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    
    class Object
    {
    };
    
    class Objects
    {
    public:
        void addObject(std::weak_ptr<Object> obj)
        {
            auto pos = std::find_if
            (
                objects_.begin(), objects_.end(),
                [&obj](const auto& our_obj)
                {
                    return obj.lock() == our_obj.lock();
                }
            );
            if (pos == objects_.end())
            {
                std::cout << "Object added" << std::endl;
                objects_.push_back(obj);
            }
        }
    private:
        std::vector<std::weak_ptr<Object>> objects_;
    };
    
    int main()
    {
        Objects obj_coll;
        std::shared_ptr<Object> obj1 = std::make_shared<Object>();
        obj_coll.addObject(obj1);
        std::shared_ptr<Object> obj2 = std::make_shared<Object>();
        obj_coll.addObject(obj2);
        obj_coll.addObject(obj1);
    }
    

    output:

    Object added
    Object added
    

    Example