I've been making some objects using the pimpl idiom, but I'm not sure whether to use std::shared_ptr
or std::unique_ptr
.
I understand that std::unique_ptr
is more efficient, but this isn't so much of an issue for me, as these objects are relatively heavyweight anyway so the cost of std::shared_ptr
over std::unique_ptr
is relatively minor.
I'm currently going with std::shared_ptr
just because of the extra flexibility. For example, using a std::shared_ptr
allows me to store these objects in a hashmap for quick access while still being able to return copies of these objects to callers (as I believe any iterators or references may quickly become invalid).
However, these objects in a way really aren't being copied, as changes affect all copies, so I was wondering that perhaps using std::shared_ptr
and allowing copies is some sort of anti-pattern or bad thing.
Is this correct?
See Question&Answers more detail:os