Search code examples
c++shared-ptr

Storing boost_shared pointers in a vector - Is it expensive


I know that vectors tend to make a copy of all objects pushed into them. My question is whether it would make sense to store a pointer to a boost::shared_ptr in a vector rather than the shared ptr itself


Solution

  • The only 100% correct answer to any performance questions is "profile and see."

    However, in this particular case, you should just keep the shared_ptrs in there directly (by value). The overhead of copying a shared pointer is low. And if your compiler supports moves, it will even be moved instead - although then you'd probably be using std::shared_ptr, right?

    How would you do it to store just pointers in there, anyway? Allocate the shared_ptrs dynamically? How would you control their own ownership?