Skip to content

Use 'requires' keyword to support C++20 features#666

Open
yumeno-yan wants to merge 1 commit intotaskflow:masterfrom
yumeno-yan:replace_SFINAE_with_requires
Open

Use 'requires' keyword to support C++20 features#666
yumeno-yan wants to merge 1 commit intotaskflow:masterfrom
yumeno-yan:replace_SFINAE_with_requires

Conversation

@yumeno-yan
Copy link
Contributor

This pull request updates the code to use C++20's requires keyword in place of SFINAE-based constraints. This change brings several benefits:

  1. Improved Readability: requires offers a cleaner and more intuitive way to express template constraints. In contrast, SFINAE requires constraints to be embedded within the template parameter list using enable_if_t and a special nullptr. By separating the constraints from the template parameters, requires improves code clarity and makes the intention of the template easier to understand.

  2. Enhanced Type Safety: Using requires allows the compiler to more easily validate template parameters against the specified conditions, providing better type-checking and more precise error messages during compilation. When a constraint fails, the compiler can now show exactly which concept check failed, making it easier to pinpoint and fix issues. This helps catch potential errors early, reducing the likelihood of subtle bugs that might arise from more complex SFINAE conditions.

  3. Aligning with C++20 Standards: By using requires, the code is more in line with modern C++ standards. This approach embraces C++20 features, ensuring that the code remains up-to-date and takes advantage of the latest language improvements.

@tsung-wei-huang
Copy link
Member

@yumeno-yan thank you for this pull request - and sorry for the delay. This is an amazing change as it simplifies a lot of meta programming. However, I will keep this open now as doing this will confuse the doxygen when generating correct template tags. We plan to migrate everything to C++20 in the near future (v4.0)

@tsung-wei-huang tsung-wei-huang added the enhancement New feature or request label May 10, 2025
@tsung-wei-huang
Copy link
Member

This is a good pull. However, the current doxygen is not able to recognize the keyword require and thus cannot generate docs correctly. Will delay this off until doxygen is capable of doing it.

@bangerth
Copy link
Contributor

Since all of the places using requires are guarded by

#if __cplusplus >= TF_CPP20

you could extend these guards to

#if __cplusplus >= TF_CPP20 && !DOXYGEN

to make sure that doxygen never sees the requires clauses, even though the compiler does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants