Skip to content

Commit a6796e6

Browse files
committed
Add is overload for free function predicate
Current implementation of `is` does not handle generic functions as predicates. The following case works: ```cpp less_than_10_a: (i : int) -> bool = i < 10; main: () = { 12 is (less_than_10_a); } ``` It work also when function has generic return type ```cpp less_than_10_b: (i : int) -> _ = i < 10; main: () = { 12 is (less_than_10_b); } ``` It does not work when function has generic argument. The following case does not work: ```cpp less_than_10_c: (i) -> _ = i < 10; main: () = { 12 is (less_than_10_c); // error } ``` This change introduce additional overload for `is` making free function predicates acceptable by `is` (the function needs to return `bool` or generic type). That makes the following cases to work: ```cpp less_than_10_c: (i) -> _ = i < 10; less_than_10_d: (i) -> bool = i < 10; main: () = { 5 is (less_than_10_c); // works 2 is (less_than_10_d); // works } ``` Functions that return other type then `bool` or `auto` do not work.
1 parent ecd3726 commit a6796e6

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

include/cpp2util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,12 @@ inline constexpr auto is( auto const& x, auto const& value ) -> bool
894894
return false;
895895
}
896896

897+
// Free function predicate case
898+
template< typename X >
899+
constexpr auto is( X const& x, bool (&value)(X const&) ) -> bool
900+
{
901+
return value(x);
902+
}
897903

898904
//-------------------------------------------------------------------------------------------------------------
899905
// Built-in as

0 commit comments

Comments
 (0)