Skip to content

Commit 1c828d0

Browse files
author
Ryan Haining
committed
Adds move-only callable support to filter
Issue #89
1 parent 420583c commit 1c828d0

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

cppitertools/filter.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ namespace iter {
2929

3030
template <typename FilterFunc, typename Container>
3131
class iter::impl::Filtered {
32+
static_assert(!std::is_reference_v<FilterFunc>);
33+
3234
private:
3335
Container container_;
3436
mutable FilterFunc filter_func_;
@@ -39,7 +41,7 @@ class iter::impl::Filtered {
3941
// Value constructor for use only in the filter function
4042
Filtered(FilterFunc filter_func, Container&& container)
4143
: container_(std::forward<Container>(container)),
42-
filter_func_(filter_func) {}
44+
filter_func_(std::move(filter_func)) {}
4345

4446
public:
4547
Filtered(Filtered&&) = default;

test/test_filter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ TEST_CASE("filter: handles different callable types", "[filter]") {
2525
REQUIRE(v == vc);
2626
}
2727

28+
SECTION("with move-only callable object") {
29+
auto f = filter(MoveOnlyLessThanValue{5}, ns);
30+
Vec v(std::begin(f), std::end(f));
31+
REQUIRE(v == vc);
32+
}
33+
2834
SECTION("with lambda") {
2935
auto ltf = [](int i) { return i < 5; };
3036
auto f = filter(ltf, ns);

0 commit comments

Comments
 (0)