【问题标题】:Filtering a container ( std::vector ) of std::shared_ptr to a container of std::weak_ptr将 std::shared_ptr 的容器 ( std::vector ) 过滤为 std::weak_ptr 的容器
【发布时间】:2014-05-15 05:28:58
【问题描述】:

我正在尝试过滤 shared_ptr 的容器,并尝试将过滤后的内容保存在非拥有容器(weak_ptr)中。下面发现的程序崩溃了。有人能看出我错过了什么吗?

    #include <memory>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <functional>

    #include <boost/bind.hpp>

    struct A
    {
        int a_val;

        explicit A():a_val(0)           { std::cout << "A default constructed\n"; }
        explicit A(int a):a_val(a)      { std::cout << "A argument constructed\n";  }
        A(const A& other)               { a_val = other.a_val ; std::cout << "A copy constructed\n";  } 
        A& operator=(const A& other)    { a_val = other.a_val ; std::cout << "A copy assigned\n";   return *this; } 
        ~A()                            { std::cout << "A is destroyed\n"; }



        struct a_visitor
        {
            std::vector < std::weak_ptr < A >  > filtered_a;

            a_visitor() { filtered_a.resize(0); }

            void operator() (std::shared_ptr < A > a_ref)
            {
                if(a_ref->a_val % 2 )
                    filtered_a.push_back(a_ref);
            }
        };

        std::function < void ( std::shared_ptr < A > ) > a_visit_function;


        void visit_vector ( a_visitor *visitor)
        {
            a_visit_function = boost::bind(&a_visitor::operator(), visitor, _1);
            std::shared_ptr< A > current_node(this);
            a_visit_function(current_node);
        }

    };

    int main(void)
    {

        std::vector < std::shared_ptr < A > > a_array;

        for(int i=10;i<20;i++)
        {
            a_array.emplace_back(std::make_shared<A>(A(i)));
        }
        std::cout << "--------------------------\n";
        std::for_each(a_array.begin(), a_array.end(), ([&]( std::shared_ptr<A> &a_ref){
            std::cout << a_ref << " : " << a_ref->a_val << std::endl;
        }));
        std::cout << "--------------------------\n";

        A::a_visitor visitor;
        std::for_each(a_array.begin(), a_array.end(), ([&](std::shared_ptr < A > a_ref){
            a_ref->visit_vector(&visitor);
        }));

        std::cout << "--------------------------\n";
        std::for_each(visitor.filtered_a.begin(), visitor.filtered_a.end(), ([&](std::weak_ptr<A> &a_ref){
            std::cout << a_ref.lock()->a_val << std::endl;
        }));
        std::cout << "--------------------------\n";
    }

【问题讨论】:

    标签: c++ shared-ptr weak-ptr


    【解决方案1】:

    这是问题所在:std::shared_ptr&lt; A &gt; current_node(this);。默认情况下你不能这样做,this 有它自己的生命周期。见:std::shared_ptr of this

    【讨论】:

    • 感谢您将我指向 enable_shared_for_this。我以前不知道。现在的设计好看吗?我试图让访问者包含对容器中对象的非拥有引用。有什么 cmets 吗?
    • 如果你访问 shared_ptr 而不是 shared_ptr 访问向量怎么办?然后像`a_visitor访问者一样调用; for_each(array.begin(), array.end(), visitor);
    • 我不确定我是否得到你的建议@Anycorn。你能补充更多吗?
    • @balasbellobas 在您的设计中,A 对象是访问者。更改您的设计,使 A 对象成为被访问的对象。
    猜你喜欢
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多