AFAIK unique_ptr
is quite tricky to use with PIMPL, since deleter is part of unique_ptr
type so it will not work with incomplete types. On the other hand shared_ptr
uses dynamic deleter so it works with incomplete types.
shared_ptr
has the performance issue of giving me an atomic operations no matter if I need it or not.
Are there any other faster alternatives in std::
I can use? I am obviously fine with type erasure, I am talking about cost of atomic refcount.
#include <any>
#include <memory>
#include <iosfwd>
std::shared_ptr<std::fstream> sp;
// unique_ptr requires complete type
// std::unique_ptr<std::fstream> up;
std::any a;
#include <fstream>
int main() {
// any requires copy_constructible
// a = std::fstream{};
sp = std::make_shared<std::fstream>();
}
notes:
- I considered
any
, but it does not work for some types. - I considered unique_ptr with dynamic deleter, but AFAIK
unique_ptr
constructor will never "tell" the deleter what is the constructed object(to give a way to deleter to learn how to destroy the object).
P.S. I know long time ago boost::shared_ptr
had macro to disable atomic refcount, but even if that is still supported I do not want to switch to boost::shared_ptr
.