c++ - Why isn't observer_ptr zeroed after a move? -


why isn't observer_ptr zeroed after move operation?

it correctly set nullptr in default construction, , make sense (and prevents pointing garbage).

and, accordingly, should zeroed when std::move()'d from, std::string, std::vector, etc.

this make candidate in several contexts raw pointers make sense, automatic generation of move operations on classes raw pointer data members, in this case.


edit

as @jonathanwakely pointed out in comments (and related aforementioned question):

if observer_ptr null after move can used implement rule of 0 types have pointer member. it's useful feature.

it seems many people miss point , utility of idea @ first.

consider:

template<typename mutex> class unique_lock {   mutex* pm;  public:   unique_lock() : pm() { }    unique_lock(mutex& m) : pm(&m) { }    ~unique_lock() { if (pm) pm->unlock(); }    unique_lock(unique_lock&& ul) : pm(ul.pm) { ul.pm = nullptr; }    unique_lock& operator=(unique_lock&& ul)   {     unique_lock(std::move(ul)).swap(*this);     return *this;   }    void swap(unique_lock& ul) { std::swap(pm, ul.pm); } }; 

with "dumb" smart pointer null-on-default-construction , null-after-move can default 3 of special member functions, becomes:

template<typename mutex> class unique_lock {   tidy_ptr<mutex> pm;  public:   unique_lock() = default;                            // 1    unique_lock(mutex& m) : pm(&m) { }    ~unique_lock() { if (pm) pm->unlock(); }    unique_lock(unique_lock&& ul) = default;            // 2    unique_lock& operator=(unique_lock&& ul) = default; // 3    void swap(unique_lock& ul) { std::swap(pm, ul.pm); } }; 

that's why it's useful have dumb, non-owning smart pointer null-after-move, tidy_ptr

but observer_ptr null-on-default-construction, if standardized useful declaring function take non-owning pointer, won't useful classes 1 above, i'll still need another non-owning dumb pointer type. having 2 non-owning dumb smart pointer types seems worse having none!


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -