6 ms·
the standard library has enable_shared_from_this, an opt-in base that allows safely constructing a shared pointer from a raw pointer, implicitly enabling intrus
by gpderetta 7y ago
the standard library has enable_shared_from_this, an opt-in base that allows safely constructing a shared pointer from a raw pointer, implicitly enabling intrusive counting. Implicit conversion to raw pointer woudl just extremely unsafe though:
std::shared_ptr<int> x {new int(0)};
{
std::unique_ptr<int> y{x}; // not actually allowed
} // boom
- pianoben 7y agoThat's true! This is one of those instances of parallel evolution. The MSO construct predates even Boost's `enable_shared_from_this`, as I recall. FWIW when I was there, new code was encouraged to use STL smart pointers where possible - people were always looking for opportunities to shed legacy patterns where we could. I totally agree about the danger of implicit conversion to raw pointers - it breaks the whole point of smart pointers. The reason it makes sense for Office is that huge swathes of code are written in "c++99" style - no exceptions, all functions return HRESULT error codes, and most functions are very explicit about who owns what. By convention, functions accepting pointers almost (?) never hang on to those pointers. Those functions that _do_ take ownership of (or retain an interest in) a pointer argument, explicitly ask for a smart pointer. Like I said, not really suitable for general use, but darn convenient _within the context of Office_.