From: Wolfgang Bangerth Date: Fri, 6 Sep 2024 23:12:58 +0000 (-0600) Subject: Ensure SmartPointer gives useful compiler errors for types not derived from Subscriptor. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F17679%2Fhead;p=dealii.git Ensure SmartPointer gives useful compiler errors for types not derived from Subscriptor. --- diff --git a/include/deal.II/base/smartpointer.h b/include/deal.II/base/smartpointer.h index 50197b318f..23066f3946 100644 --- a/include/deal.II/base/smartpointer.h +++ b/include/deal.II/base/smartpointer.h @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -86,6 +87,8 @@ DEAL_II_NAMESPACE_OPEN * to a constant object (disallowing write access when dereferenced), while * `SmartPointer` is a mutable pointer. * + * @dealiiConceptRequires{std::is_base_of_v} + * * @ingroup memory */ template @@ -268,7 +271,11 @@ inline SmartPointer::SmartPointer() : pointer(nullptr) , id(typeid(P).name()) , pointed_to_object_is_alive(false) -{} +{ + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); +} @@ -278,6 +285,10 @@ inline SmartPointer::SmartPointer(T *t) , id(typeid(P).name()) , pointed_to_object_is_alive(false) { + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); + if (t != nullptr) t->subscribe(&pointed_to_object_is_alive, id); } @@ -290,6 +301,10 @@ inline SmartPointer::SmartPointer(T *t, const std::string &id) , id(id) , pointed_to_object_is_alive(false) { + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); + if (pointer != nullptr) pointer->subscribe(&pointed_to_object_is_alive, id); } @@ -303,6 +318,10 @@ inline SmartPointer::SmartPointer(const SmartPointer &other) , id(other.id) , pointed_to_object_is_alive(false) { + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); + if (other != nullptr) { Assert(other.pointed_to_object_is_alive, @@ -320,6 +339,10 @@ inline SmartPointer::SmartPointer(const SmartPointer &other) , id(other.id) , pointed_to_object_is_alive(false) { + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); + if (other != nullptr) { Assert(other.pointed_to_object_is_alive, @@ -337,6 +360,10 @@ inline SmartPointer::SmartPointer(SmartPointer &&other) noexcept , id(other.id) , pointed_to_object_is_alive(false) { + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); + if (other != nullptr) { Assert(other.pointed_to_object_is_alive, @@ -367,6 +394,10 @@ inline SmartPointer::SmartPointer(SmartPointer &&other) noexcept template inline SmartPointer::~SmartPointer() { + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); + if (pointed_to_object_is_alive && pointer != nullptr) pointer->unsubscribe(&pointed_to_object_is_alive, id); }