Just use a std::shared_ptr<Foo>
.
While it is rarely useful, you can in fact construct a non-owning non-counting std::shared_ptr
:
auto p = std::shared_ptr<Foo>{std::shared_ptr<void>(), raw_pointer};
If you want to cater to weird people disrespecting the abstraction (looking at the reference-counts, to be specific), you could also stash an eternal anchor somewhere and use that:
struct pass_t {
template <class... T>
constexpr int operator()(T&&...) noexcept
{ return 0; }
};
constexpr inline pass_t pass;
inline const std::shared_ptr<void> anchor {nullptr, pass};
auto p = std::shared_ptr<Foo>{anchor, raw_pointer};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…