-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor(parented_ptr): make trivially destructible in release mode, delete move operations #11981
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,14 +26,21 @@ class parented_ptr final { | |
explicit parented_ptr(T* t) noexcept | ||
: m_ptr{t} { | ||
} | ||
|
||
// Only generate destructor if not empty, otherwise its empty but will | ||
// cause the parented_ptr to be not trivially destructible even though it could be. | ||
#ifdef MIXXX_DEBUG_ASSERTIONS_ENABLED | ||
~parented_ptr() noexcept { | ||
DEBUG_ASSERT(!m_ptr || static_cast<const QObject*>(m_ptr)->parent()); | ||
} | ||
|
||
// Delete copy constructor and copy assignment operator | ||
#else | ||
// explicitly generate trivial destructor (since decltype(m_ptr) is not a class type) | ||
~parented_ptr() noexcept = default; | ||
#endif | ||
// Rule of 5 | ||
parented_ptr(const parented_ptr<T>&) = delete; | ||
parented_ptr& operator=(const parented_ptr<T>&) = delete; | ||
parented_ptr(const parented_ptr<T>&& other) = delete; | ||
parented_ptr& operator=(const parented_ptr<T>&& other) = delete; | ||
Comment on lines
40
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have never understood why it should be not lowed to copy or move a parented_ptr() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, It makes sense to have only one instance checking the ownership. The deleted copy constructor enforces that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't really check to be honest. I just saw that we didn't follow the rule of five and thus I chose the safe default. We can lift the restrictions if we can be sure that its safe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have reservations about moving. If we move an object that is owned by its parent, its only safe if the parent knows that it has been moved, do QObjects take care of telling their parent? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is probably OK, we can change it whenever we find a valid use case for it. |
||
|
||
// If U* is convertible to T* then parented_ptr<U> is convertible to parented_ptr<T> | ||
template< | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for my understanding '~parented_ptr() = default' and
~parented_ptr() {}
is the same. So I can imagine that this is not needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No its not equivalent. One if an empty definition which means that its still user provided even if it does nothing, the other one is the default declared one which does not count as user-provided.
See
https://en.cppreference.com/w/cpp/language/destructor#Trivial_destructor
https://compiler-explorer.com/z/cbG99jbzr