From 35a0fd3e8e07864cbdc733c65a3b53e0ae25c5cd Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Thu, 16 Jul 2020 09:42:12 +0200 Subject: [PATCH] ENH: reset tmp via assignment from literal nullptr (#1775) - previously this was marked as '= delete' for consistency with assignment from an empty pointer being a runtime error. However, these can be considered semantically different and it makes sense to permit this as equivalent to reset(nullptr). This change does not break existing code since the operator was previously unavailable (deleted). STYLE: refactor tmp operator=(T*) - delegate to reset() after initial checks --- src/OpenFOAM/memory/tmp/tmp.H | 5 ++--- src/OpenFOAM/memory/tmp/tmpI.H | 12 ++++++++---- src/OpenFOAM/memory/tmp/tmpNrc.H | 5 ++--- src/OpenFOAM/memory/tmp/tmpNrcI.H | 12 ++++++++---- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H index 6369b5aa2be..5aef154bd8e 100644 --- a/src/OpenFOAM/memory/tmp/tmp.H +++ b/src/OpenFOAM/memory/tmp/tmp.H @@ -255,9 +255,8 @@ public: // Fatal for a null pointer, or when the pointer is non-unique. inline void operator=(T* p); - //- No assignment from literal nullptr. - // Consistent with run-time check for nullptr on assignment. - void operator=(std::nullptr_t) = delete; + //- Reset via assignment from literal nullptr + inline void operator=(std::nullptr_t) noexcept; }; diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index 10f370ae517..53fa27f1a78 100644 --- a/src/OpenFOAM/memory/tmp/tmpI.H +++ b/src/OpenFOAM/memory/tmp/tmpI.H @@ -464,8 +464,6 @@ inline void Foam::tmp<T>::operator=(tmp<T>&& other) noexcept template<class T> inline void Foam::tmp<T>::operator=(T* p) { - clear(); - if (!p) { FatalErrorInFunction @@ -480,8 +478,14 @@ inline void Foam::tmp<T>::operator=(T* p) << abort(FatalError); } - ptr_ = p; - type_ = PTR; + reset(p); +} + + +template<class T> +inline void Foam::tmp<T>::operator=(std::nullptr_t) noexcept +{ + reset(nullptr); } diff --git a/src/OpenFOAM/memory/tmp/tmpNrc.H b/src/OpenFOAM/memory/tmp/tmpNrc.H index 5dc589e156d..833f94a69c7 100644 --- a/src/OpenFOAM/memory/tmp/tmpNrc.H +++ b/src/OpenFOAM/memory/tmp/tmpNrc.H @@ -234,9 +234,8 @@ public: // Fatal for a null pointer inline void operator=(T* p); - //- No assignment from literal nullptr. - // Consistent with run-time check for nullptr on assignment. - void operator=(std::nullptr_t) = delete; + //- Reset via assignment from literal nullptr + inline void operator=(std::nullptr_t) noexcept; //- Conversion to tmp - releases pointer or copies reference inline operator tmp<T>(); diff --git a/src/OpenFOAM/memory/tmp/tmpNrcI.H b/src/OpenFOAM/memory/tmp/tmpNrcI.H index def9c1a67ed..19046f3c3f2 100644 --- a/src/OpenFOAM/memory/tmp/tmpNrcI.H +++ b/src/OpenFOAM/memory/tmp/tmpNrcI.H @@ -414,8 +414,6 @@ inline void Foam::tmpNrc<T>::operator=(tmpNrc<T>&& other) noexcept template<class T> inline void Foam::tmpNrc<T>::operator=(T* p) { - clear(); - if (!p) { FatalErrorInFunction @@ -423,8 +421,14 @@ inline void Foam::tmpNrc<T>::operator=(T* p) << abort(FatalError); } - ptr_ = p; - type_ = PTR; + reset(p); +} + + +template<class T> +inline void Foam::tmpNrc<T>::operator=(std::nullptr_t) noexcept +{ + reset(nullptr); } -- GitLab