Is using std::vector< std> > an antipattern?

后端 未结 4 1422
醉话见心
醉话见心 2020-12-29 22:22

For a long time I was using std::vector and std::shared_ptr hand in hand. Recently I started using std::shared_ptr when

4条回答
  •  醉酒成梦
    2020-12-29 23:07

    I would suggest reviewing your design with a view to establish a clear owner of those object. This is the absence of clear ownership that lead people to use shared smart pointers.

    Bjarne Stroustrup recommends using smart pointers only as a last resort. His recommendations (best to worst) are:

    1. Store an object by value.
    2. Store many objects in a container by value.
    3. If nothing else works, use smart pointers.

    See Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11, and C++14 at 0:37:40.

提交回复
热议问题