-
Notifications
You must be signed in to change notification settings - Fork 259
Extension of is() part1: free function predicates, fix implicit cast for build-in and custom is operator, refactoring #1053
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This change address to issues: 1. implicit downcasting of arguments, 2. add support for predicates with generic type argument 1. Currently, if is() is checked against predicate the predicate argument can be implicitly casted that can lead to downcasting double to int equal_pi: (x:int) -> bool = { return x == 3; }; d := 3.14; if d is (equal_pi) { // d will be casted to int and will return true // ... } After this change downcasting will not happen no false positive/negative matches will happen. 2. Currently, if is() is checked against predicate with generic type it does not compile. After this change the code will compile and work as expected.
Adding test for using is with lambdas, and generic lambdas.
Adjust to new style of using one function for a match and series of constexpr ifs. There is a possibility to extend is() for matching std::integer_sequence but unfortunatelly clang is not support it (gcc & msvc do). template <template <typename, typename, typename...> class C, typename X> constexpr auto is( X const& ) { if constexpr (specialization_of_template<X, C>) { return std::true_type{}; } else { return std::false_type{}; } } template <template <typename, auto...> class C, typename X> constexpr auto is( X const& ) { if constexpr (specialization_of_template_type_and_nttp<X, C>) { return std::true_type{}; } else { return std::false_type{}; } } Alternatively we can support more matches for gcc & msvc by providing: #if defined(__clang__) template <template <typename...> class C, typename X> #else // allow us to support std::integer_sequence on gcc and msvc template <template <typename, typename, typename...> class C, typename X> #endif constexpr auto is( X const& ) { if constexpr (specialization_of_template<X, C>) { return std::true_type{}; } else { return std::false_type{}; } } #if defined(__clang__) template <template <typename, auto> class C, typename X> #else // allow us to support std::integer_sequence on gcc and msvc template <template <typename, auto...> class C, typename X> #endif constexpr auto is( X const& ) { if constexpr (specialization_of_template_type_and_nttp<X, C>) { return std::true_type{}; } else { return std::false_type{}; } }
In current implementation custom is operator (`is_op()` member function) suffers from implicit casting of its argument. The same issue that were with function predicates. After this change we added the check if the cast is lossless or if the custom is operator is generic function. Added tests to verify is with various values. Added new concepts to make checks more readable: - valid_custom_is_operator - defined - to check if type is defined (used to determined generic fun) - predicate_member_fun - to check if member function is a predicate, Added new type trait: - argument_of_op_is_t to determined the type of op_is argument, Correct helper methods: - argument_of_helper - to handle ref-qualified member functions, - argument_of_helper - to work with non-copyable types,
Thanks! I'm now catching up after the April madness and aim to review this PR in the next few days. Thanks again for this. Do we know why some regression tests are failing -- are they just out of date? |
Add an array value-equality case (thanks to MSVC for warning about that) Also stop forwarding the `value` to `std::vformat` to stay compatible with the `vformat` change
I just took a pass over it and tweaked a couple of small things (mostly things only MSVC reported or was affected by) -- looks good! Thanks. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is the first part of
is()
rework originally done in #701 .It contains: