Skip to content

documented non-const reference passing to erase_if #312

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions doc/modules/ROOT/pages/reference/concurrent_flat_map.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,9 @@ Erases the element `x` with key equivalent to `k` if it exists and `f(x)` is `tr
[horizontal]
Returns:;; The number of elements erased (0 or 1).
Throws:;; Only throws an exception if it is thrown by `hasher`, `key_equal` or `f`.
Notes:;; The `template<class K, class F>` overload only participates in overload resolution if `std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>` is `false`. +
Notes:;; `f` is passed a non-const reference to `x`. +
+
The `template<class K, class F>` overload only participates in overload resolution if `std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>` is `false`. +
+
The `template<class K, class F>` overload only participates in overload resolution if `Hash::is_transparent` and `Pred::is_transparent` are valid member typedefs. The library assumes that `Hash` is callable with both `K` and `Key` and that `Pred` is transparent. This enables heterogeneous lookup which avoids the cost of instantiating an instance of the `Key` type.

Expand All @@ -1440,7 +1442,7 @@ The `template<class K, class F>` overload only participates in overload resoluti
template<class F> size_type erase_if(F f);
```

Successively invokes `f` with references to each of the elements in the table, and erases those for which `f` returns `true`.
Successively invokes `f` with non-const references to each of the elements in the table, and erases those for which `f` returns `true`.

[horizontal]
Returns:;; The number of elements erased.
Expand All @@ -1453,7 +1455,7 @@ Throws:;; Only throws an exception if it is thrown by `f`.
template<class ExecutionPolicy, class F> void erase_if(ExecutionPolicy&& policy, F f);
```

Invokes `f` with references to each of the elements in the table, and erases those for which `f` returns `true`.
Invokes `f` with non-const references to each of the elements in the table, and erases those for which `f` returns `true`.
Execution is parallelized according to the semantics of the execution policy specified.

[horizontal]
Expand Down
8 changes: 5 additions & 3 deletions doc/modules/ROOT/pages/reference/concurrent_node_map.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,9 @@ Erases the element `x` with key equivalent to `k` if it exists and `f(x)` is `tr
[horizontal]
Returns:;; The number of elements erased (0 or 1).
Throws:;; Only throws an exception if it is thrown by `hasher`, `key_equal` or `f`.
Notes:;; The `template<class K, class F>` overload only participates in overload resolution if `std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>` is `false`. +
Notes:;; `f` is passed a non-const reference to `x`. +
+
The `template<class K, class F>` overload only participates in overload resolution if `std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>` is `false`. +
+
The `template<class K, class F>` overload only participates in overload resolution if `Hash::is_transparent` and `Pred::is_transparent` are valid member typedefs. The library assumes that `Hash` is callable with both `K` and `Key` and that `Pred` is transparent. This enables heterogeneous lookup which avoids the cost of instantiating an instance of the `Key` type.

Expand All @@ -1530,7 +1532,7 @@ The `template<class K, class F>` overload only participates in overload resoluti
template<class F> size_type erase_if(F f);
```

Successively invokes `f` with references to each of the elements in the table, and erases those for which `f` returns `true`.
Successively invokes `f` with non-const references to each of the elements in the table, and erases those for which `f` returns `true`.

[horizontal]
Returns:;; The number of elements erased.
Expand All @@ -1543,7 +1545,7 @@ Throws:;; Only throws an exception if it is thrown by `f`.
template<class ExecutionPolicy, class F> void erase_if(ExecutionPolicy&& policy, F f);
```

Invokes `f` with references to each of the elements in the table, and erases those for which `f` returns `true`.
Invokes `f` with non-const references to each of the elements in the table, and erases those for which `f` returns `true`.
Execution is parallelized according to the semantics of the execution policy specified.

[horizontal]
Expand Down
2 changes: 2 additions & 0 deletions doc/modules/ROOT/pages/reference/unordered_flat_map.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,8 @@ for (auto i = c.begin(), last = c.end(); i != last; ) {
}
return original_size - c.size();
```
+
Note that the references passed to `pred` are non-const.

=== Serialization

Expand Down
4 changes: 3 additions & 1 deletion doc/modules/ROOT/pages/reference/unordered_map.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,9 @@ for (auto i = c.begin(), last = c.end(); i != last; ) {
}
}
return original_size - c.size();
```
```
+
Note that the references passed to `pred` are non-const.

=== Serialization

Expand Down
2 changes: 2 additions & 0 deletions doc/modules/ROOT/pages/reference/unordered_multimap.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,8 @@ for (auto i = c.begin(), last = c.end(); i != last; ) {
}
return original_size - c.size();
```
+
Note that the references passed to `pred` are non-const.

=== Serialization

Expand Down
2 changes: 2 additions & 0 deletions doc/modules/ROOT/pages/reference/unordered_node_map.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,8 @@ for (auto i = c.begin(), last = c.end(); i != last; ) {
}
return original_size - c.size();
```
+
Note that the references passed to `pred` are non-const.

=== Serialization

Expand Down
30 changes: 30 additions & 0 deletions test/unordered/erase_if.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2021-2022 Christian Mazakas.
// Copyright 2025 Joaquin M Lopez Munoz.
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

Expand Down Expand Up @@ -42,8 +43,35 @@ namespace test {
}
};

template <class T>
struct non_const_pred;

template<class T, class U>
struct non_const_pred<std::pair<const T, U> >
{
bool operator()(std::pair<const T, U>& x) const
{
U u = std::move(x.second);
(void)u;
return true;
}
};

} // namespace test

template <class UnorderedMap> void test_map_nonconst_erase_if()
{
typedef UnorderedMap map_type;
typedef typename map_type::value_type value_type;
typedef typename map_type::size_type size_type;

map_type m;
m.insert(value_type());
size_type num_erased = erase_if(m, test::non_const_pred<value_type>());
BOOST_TEST(m.empty());
BOOST_TEST_EQ(num_erased, 1u);
}

template <class UnorderedMap> void test_map_erase_if()
{
typedef UnorderedMap map_type;
Expand Down Expand Up @@ -71,6 +99,8 @@ template <class UnorderedMap> void test_map_erase_if()
num_erased = erase_if(map, test::is_even());
BOOST_TEST_EQ(map.size(), 2u);
BOOST_TEST_EQ(num_erased, size - map.size());

test_map_nonconst_erase_if<map_type>();
}

template <class UnorderedSet> void test_set_erase_if()
Expand Down
Loading