Problems with shared_ptr wrapping a dynamic array

后端 未结 4 1466
遇见更好的自我
遇见更好的自我 2021-01-13 02:08

I wanted to replace some raw pointers in my class with a std::shared_ptr so that I don\'t have to worry when I create copies of that class. But the raw pointers

4条回答
  •  庸人自扰
    2021-01-13 02:45

    std::unique_ptr is specialized for array types so you can use T[] with it and it will know that it's still just storing a T*. std::shared_ptr is not specialized this way and so a shared_ptr will try to store a pointer to an array, T(*)[], which won't work very well with the conventions around raw arrays in C++. Not to mention that an array of unknown size is an incomplete type, and shared_ptr will eventually need a complete type.

    You mention knowing that std::vector should be a better solution but doesn't perform as well. It should perform just fine and you'd probably be better off figuring out why it doesn't.

提交回复
热议问题