mirror of
https://github.com/OPM/ResInsight.git
synced 2026-10-25 22:50:25 -05:00
PdmObjectCollection: Move PdmObject base check to constructor
Replace the class-level static_assert with a DerivedFromPdmObject concept checked inside the constructor body. The constructor is lazily instantiated, so T can be forward-declared at the point PdmObjectCollection<T> is used as a base class.
This commit is contained in:
1 parent
5e880ffc26
commit
aaa95bdcf3
2 files changed
+16
-3
No files matched your search
@@ -21,11 +21,24 @@
|
||||
#include "cafPdmChildArrayField.h"
|
||||
#include "cafPdmObject.h"
|
||||
|
||||
#include <concepts>
|
||||
#include <vector>
|
||||
|
||||
namespace caf
|
||||
{
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
/// Constrains a template parameter to types deriving from caf::PdmObject.
|
||||
///
|
||||
/// Checked via `static_assert` inside a lazily-instantiated member function (the constructor)
|
||||
/// rather than as a class-template constraint, so that T may be forward-declared at the point
|
||||
/// the class template is instantiated as a base class.
|
||||
///
|
||||
//==================================================================================================
|
||||
template <typename T>
|
||||
concept DerivedFromPdmObject = std::derived_from<T, PdmObject>;
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
/// Generic templated collection base class for managing collections of PDM objects
|
||||
@@ -42,8 +55,6 @@ namespace caf
|
||||
template <typename T>
|
||||
class PdmObjectCollection : public PdmObject
|
||||
{
|
||||
static_assert( std::is_base_of<caf::PdmObject, T>::value, "T must inherit from caf::PdmObject" );
|
||||
|
||||
public:
|
||||
// Collection access
|
||||
std::vector<T*> items() const;
|
||||
@@ -89,4 +100,4 @@ private:
|
||||
} // namespace caf
|
||||
|
||||
// Include template implementation
|
||||
#include "cafPdmObjectCollection.inl"
|
||||
#include "cafPdmObjectCollection.inl"
|
||||
@@ -29,6 +29,8 @@ namespace caf
|
||||
template <typename T>
|
||||
PdmObjectCollection<T>::PdmObjectCollection()
|
||||
{
|
||||
static_assert( DerivedFromPdmObject<T>, "T must inherit from caf::PdmObject" );
|
||||
|
||||
// m_items field must be initialized by derived class using CAF_PDM_InitFieldNoDefault
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user