Skip to content
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

Constrain the Property object at compile-time #811

Merged
merged 4 commits into from Jul 28, 2023

Conversation

antonysigma
Copy link
Contributor

Description

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.

Resolves feature #810 .

How to test this?

It is a compile-time type checking. If the production code or test code compiles, the code is valid.

Test System

  • OS: Ubuntu 20.04
  • Compiler: clang 12.0 with C++20 and/or C++11
  • Dependency versions: Irrelevant.

@codecov
Copy link

codecov bot commented Jul 21, 2023

Codecov Report

Merging #811 (7f14fdb) into master (3f005dc) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master     #811   +/-   ##
=======================================
  Coverage   84.58%   84.58%           
=======================================
  Files          68       68           
  Lines        4781     4781           
=======================================
  Hits         4044     4044           
  Misses        737      737           
Files Changed Coverage Δ
include/highfive/H5PropertyList.hpp 93.75% <ø> (ø)
include/highfive/bits/H5PropertyList_misc.hpp 76.43% <ø> (ø)

@antonysigma
Copy link
Contributor Author

Hi HighFive project owners @alkino . I noticed that the PR passes all CI validations except clang-format. Shall I revise and resubmit the PR? I know how to run clang-format locally. Just wanna be certain about who does what, before I proceed.

@1uc
Copy link
Collaborator

1uc commented Jul 21, 2023

Thank you for the nice PR. Yes, please feel free to fix the formatting; I think the version of clang-format you need is 14.0.0. Otherwise the required diff to appease CI is:

 diff --git a/include/highfive/H5PropertyList.hpp b/include/highfive/H5PropertyList.hpp
index af149a9..241c988 100644
--- a/include/highfive/H5PropertyList.hpp
+++ b/include/highfive/H5PropertyList.hpp
@@ -143,10 +143,9 @@ class PropertyListBase: public Object {
 ///
 /// \cond
 #if __cplusplus >= 202002L
-template<typename P>
-concept PropertyInterface = requires(P p, const hid_t hid)
-{
-    { p.apply(hid) };
+template <typename P>
+concept PropertyInterface = requires(P p, const hid_t hid) {
+    {p.apply(hid)};
 };

I've not had time today to compile it and see how it works myself. Though I'm looking forward to having a reason to look at concepts.

/// \sa Instructions to document C++20 concepts with Doxygen: https://github.com/doxygen/doxygen/issues/2732#issuecomment-509629967
///
/// \cond
#if __cplusplus >= 202002L
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antonysigma I would like there to be a way of forcing the concepts to be off. I'm worried a user will find a compiler that claims to be 202002L but doesn't support concepts; or has a compiler bug. If this happens they would be unable to use HighFive, because a nice, but non-essential feature is broken.

If you like please add a CMake variable HIGHFIVE_HAS_CONCEPTS which defaults to On. Then add code to instruct the compiler to add -DHIGHFIVE_HAS_CONCEPTS={0,1} has appropriate. Then, the if-condition above would be

#if HIGHFIVE_HAS_CONCEPTS && __cplusplus >= 202002L

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I am not familiar to CMake >12.0 though. Feel free to revise the changes where it make sense.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I slightly changed it and tested if it works. One might need -fconcepts-diagnostics-depth=2 to get meaningful compiler output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Let me know if you need anything else. I will standby.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to prepare a PR which modifies CI to also compile HighFive as C++20 code. Otherwise, the concepts aren't tested during CI. Then we can rebase this PR onto the other one; and everything is tested nicely again.

Thank you for this nice PR.

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.
The commit sets the variable for the target `libdeps` (and transitively
`HighFive`). Furthermore, it ensures that the value of
`HIGHFIVE_HAS_CONCEPTS` is either `0` or `1`. Finally, it removes the
check for C++ standard, because we already check if the compiler should
be able to handle C++20 features via `__cplusplus` in the code.
Copy link
Collaborator

@1uc 1uc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, nicely done!

@1uc 1uc merged commit 2f5dc7d into BlueBrain:master Jul 28, 2023
35 checks passed
@antonysigma antonysigma deleted the cpp20-concepts branch July 28, 2023 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants