Skip to content

Commit 81b5410

Browse files
committed
Generalized support callables when std::invoke is available
1 parent 72043c7 commit 81b5410

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ ones. `gfx::timsort` should be usable as a drop-in replacement for `std::stable_
1515
can't fallback to a O(n log² n) algorithm when there isn't enough extra heap memory available.
1616

1717
Additionally `gfx::timsort` can take a [projection function](https://ezoeryou.github.io/blog/article/2019-01-22-ranges-projection.html)
18-
after the comparison function. The support is a bit rougher than in the linked article or the C++20 stadard library:
19-
only instances of types callable with parentheses can be used, there is no support for pointer to members.
18+
after the comparison function. The support is a bit rougher than in the linked article or the C++20 standard library:
19+
unless `std::invoke` is available, only instances of types callable with parentheses can be used, there is no support
20+
for pointer to members.
2021

2122
This implementation of timsort notably avoids using the postfix `++` or `--` operators: only their prefix equivalents
2223
are used, which means that timsort will work even if the postfix operators are not present or return an incompatible

include/gfx/timsort.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,17 @@ struct projection_compare {
133133
#if GFX_TIMSORT_USE_STD_MOVE
134134
template <typename T, typename U>
135135
bool operator()(T &&lhs, U &&rhs) {
136+
# ifdef __cpp_lib_invoke
137+
return static_cast<bool>(std::invoke(compare,
138+
std::invoke(projection, std::forward<T>(lhs)),
139+
std::invoke(projection, std::forward<U>(rhs))
140+
));
141+
# else
136142
return static_cast<bool>(compare(
137143
projection(std::forward<T>(lhs)),
138144
projection(std::forward<U>(rhs))
139145
));
146+
# endif
140147
}
141148
#else
142149
template <typename T, typename U>

0 commit comments

Comments
 (0)