c++ - Pimpl idiom + moving? -


what standard way it?

let's have class a has data member std::unique_ptr<impl> m_impl.

for example, should class a's move assignment operator's contents this?

m_impl = std::move(other.m_impl); 

or this?

*m_impl = std::move(*other.m_impl); 

the first case more efficient second. raise few issues.

after moving, m_impl data member of other nullptr. should moved-from object throw exceptions when it's being used or let user run runtime errors due dereferencing nullptr?

for thread-safe classes mean m_impl usage needs synchronized. not sure if calling std::unique_ptr's move constructor/assignment operator thread-safe or not.

the former. when using unique_ptr, doing because object isn't copyable and/or movable in first place. moving pointee not option.

after moving, m_impl data member of other nullptr.

that correct moved-from std::unique_ptr. needs support destruction , reassignment , else undefined , not matter (segfault in case of null pointer).

for thread-safe classes mean m_impl usage needs synchronized.

standard library not thread-safe unless explicitly stated, reentrant. if object needs thread-safe, have ensure yourself.

that said if thread moves object of not sole owner, cause problems other threads, because still try access @ old location.

you lock move operations against operations on object , define semantics of operations on moved-from object return error in way, sounds big pain , confusing use.

thread-safe objects should exception. should aim handing objects on between threads 1 owns object @ given time , have shared data immutable (except possible reference count, can done reasonably performant std::atomic or std::shared_ptr, built on top of it).

if need thread-safe object, should either not movable, or should behave handle referring it's internals std::shared_ptr, is thread-safe.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -