Skip to content

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
merged 5 commits into from
May 11, 2024

Conversation

filipsajdak
Copy link
Contributor

@filipsajdak filipsajdak commented Apr 15, 2024

This is the first part of is() rework originally done in #701 .

It contains:

  • handling of free function predicates,
  • fix of implicit casts for build-in is() and custom is() operator,
  • rework of matching variable to template - adjust to new style,

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,
@hsutter
Copy link
Owner

hsutter commented May 8, 2024

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
@hsutter
Copy link
Owner

hsutter commented May 11, 2024

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.

@hsutter hsutter merged commit e6a0200 into hsutter:main May 11, 2024
13 of 18 checks passed
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.

2 participants