From 24cee49f6d6fcd9f9755a50f32d56fb733c462ab Mon Sep 17 00:00:00 2001 From: Antony Chan Date: Thu, 20 Jul 2023 10:18:01 -0700 Subject: [PATCH] Constrain the Property object at compile-time The property struct/class requires a public member function signature `apply(hid_t) const`. Describe the constrain in the C++20 concepts syntax. The primarily goal is to make compiler errors more readable for average users. Average users are those who do not understand SFINAE. --- include/highfive/H5PropertyList.hpp | 25 +++++++++++++++++-- include/highfive/bits/H5PropertyList_misc.hpp | 2 +- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/include/highfive/H5PropertyList.hpp b/include/highfive/H5PropertyList.hpp index db541a42f..af149a94a 100644 --- a/include/highfive/H5PropertyList.hpp +++ b/include/highfive/H5PropertyList.hpp @@ -133,6 +133,27 @@ class PropertyListBase: public Object { friend T details::get_plist(const U&, hid_t (*f)(hid_t)); }; +/// \interface PropertyInterface +/// \brief HDF5 file property object +/// +/// A property is an object which is expected to have a method with the +/// following signature `void apply(hid_t hid) const` +/// +/// \sa Instructions to document C++20 concepts with Doxygen: https://github.com/doxygen/doxygen/issues/2732#issuecomment-509629967 +/// +/// \cond +#if __cplusplus >= 202002L +template +concept PropertyInterface = requires(P p, const hid_t hid) +{ + { p.apply(hid) }; +}; + +#else +#define PropertyInterface typename +#endif +/// \endcond + /// /// \brief HDF5 property Lists /// @@ -149,8 +170,8 @@ class PropertyList: public PropertyListBase { /// Add a property to this property list. /// A property is an object which is expected to have a method with the /// following signature void apply(hid_t hid) const - /// - template + /// \tparam PropertyInterface + template void add(const P& property); /// diff --git a/include/highfive/bits/H5PropertyList_misc.hpp b/include/highfive/bits/H5PropertyList_misc.hpp index cf94af832..cef301e53 100644 --- a/include/highfive/bits/H5PropertyList_misc.hpp +++ b/include/highfive/bits/H5PropertyList_misc.hpp @@ -70,7 +70,7 @@ inline void PropertyList::_initializeIfNeeded() { } template -template +template inline void PropertyList::add(const P& property) { _initializeIfNeeded(); property.apply(_hid);