地图中过期的 weak_ptr 会发生什么

What happens to an expired weak_ptr in a map

我想了解 weak_ptr 已过期的地图中的条目(boost::weak_ptr 类型)会发生什么情况。地图中的相应条目是否会自动删除?

key是一个整数,对应的value是一个weak_ptr.

我写的示例代码,但无法编译

    #include <iostream>
    #include <map>
    #include <boost/enable_shared_from_this.hpp>

    using namespace std;

    class Foo : public boost::enable_shared_from_this<Foo> {
    public:
        Foo(int n = 0) : bar(n) {
            std::cout << "Foo: constructor, bar = " << bar << '\n';
        }
        ~Foo() {
            std::cout << "Foo: destructor, bar = " << bar << '\n';
        }
        int getBar() const { return bar; }
        boost::shared_ptr<Foo> inc_ref() {
            return shared_from_this();
        }
    private:
            int bar;
    };

    std::map<int, boost::weak_ptr<Foo> > mappy;

    int main()
    {
        boost::shared_ptr<Foo> sptr(new Foo(1));

        std::pair<std::map<int, boost::weak_ptr<Foo> >::iterator, bool> res = 
mappy.insert(std::make_pair(10, sptr));

        if (!res.second ) {
            cout << "key already exists "
                             << " with value " << (res.first)->second << "\n";
        } else {
            cout << "created key" << "\n";
        }

        std::cout << "sptr use count  "<< sptr.use_count() << '\n';

        sptr.reset();

        std::cout << "sptr use count  "<< sptr.use_count() << '\n';

        std::map<int, boost::weak_ptr<Foo>, std::less<int> >::iterator map_itr = mappy.find(10);
        if (map_itr == mappy.end()) {
            cout << "Entry removed" << "\n";
        } else {
            cout << "Entry found: " << map_itr << "\n";
        }

        return 0;
    }

Java 中 WeakSet 的文档说当 weak_ptr 过期时条目将被删除。所以想检查地图是否表现出类似(或未定义)的行为。

谢谢!

Does the corresponding entry in the map get automatically deleted?

不,不是。该条目将继续存在。地图和 shared_ptr 是完全不相关的实体。最后 shared_ptr 所做的所有破坏都是释放内存。

weak_ptr的好处是weak_ptr可以知道shared_ptr有没有被删除。这就是 expired() and lock() 成员函数的用途。但是,一旦它过期,您仍然可以将其从地图中删除。

Does the corresponding entry in the map get automatically deleted?

绝对不是。要实现这一点,std::map::find() 必须修改地图以删除过期元素,但由于合同原因,它不能这样做。特别是 const 版本。