Skip to content

Commit 164cbe4

Browse files
authored
documented non-const reference passing to erase_if (#312)
* documented non-const reference passing to erase_if * prevented warning as error in Boost.Container build * skipped tests for MinGW (boostorg/atomic#70) * correct reference link for previous commit is boostorg/atomic#73 * commented previous disabled cases * disabled cfoa_serialization_tests with TSAN * fixed previous * fixed previous * fixed previous
1 parent a64d81a commit 164cbe4

File tree

8 files changed

+58
-8
lines changed

8 files changed

+58
-8
lines changed

doc/modules/ROOT/pages/reference/concurrent_flat_map.adoc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,9 @@ Erases the element `x` with key equivalent to `k` if it exists and `f(x)` is `tr
14291429
[horizontal]
14301430
Returns:;; The number of elements erased (0 or 1).
14311431
Throws:;; Only throws an exception if it is thrown by `hasher`, `key_equal` or `f`.
1432-
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`. +
1432+
Notes:;; `f` is passed a non-const reference to `x`. +
1433+
+
1434+
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`. +
14331435
+
14341436
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.
14351437

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

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

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

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

14591461
[horizontal]

doc/modules/ROOT/pages/reference/concurrent_node_map.adoc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,9 @@ Erases the element `x` with key equivalent to `k` if it exists and `f(x)` is `tr
15191519
[horizontal]
15201520
Returns:;; The number of elements erased (0 or 1).
15211521
Throws:;; Only throws an exception if it is thrown by `hasher`, `key_equal` or `f`.
1522-
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`. +
1522+
Notes:;; `f` is passed a non-const reference to `x`. +
1523+
+
1524+
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`. +
15231525
+
15241526
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.
15251527

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

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

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

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

15491551
[horizontal]

doc/modules/ROOT/pages/reference/unordered_flat_map.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,8 @@ for (auto i = c.begin(), last = c.end(); i != last; ) {
15141514
}
15151515
return original_size - c.size();
15161516
```
1517+
+
1518+
Note that the references passed to `pred` are non-const.
15171519

15181520
=== Serialization
15191521

doc/modules/ROOT/pages/reference/unordered_map.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1766,7 +1766,9 @@ for (auto i = c.begin(), last = c.end(); i != last; ) {
17661766
}
17671767
}
17681768
return original_size - c.size();
1769-
```
1769+
```
1770+
+
1771+
Note that the references passed to `pred` are non-const.
17701772

17711773
=== Serialization
17721774

doc/modules/ROOT/pages/reference/unordered_multimap.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,8 @@ for (auto i = c.begin(), last = c.end(); i != last; ) {
14861486
}
14871487
return original_size - c.size();
14881488
```
1489+
+
1490+
Note that the references passed to `pred` are non-const.
14891491

14901492
=== Serialization
14911493

doc/modules/ROOT/pages/reference/unordered_node_map.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,8 @@ for (auto i = c.begin(), last = c.end(); i != last; ) {
16181618
}
16191619
return original_size - c.size();
16201620
```
1621+
+
1622+
Note that the references passed to `pred` are non-const.
16211623

16221624
=== Serialization
16231625

test/Jamfile.v2

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ run unordered/serialization_tests.cpp
156156
<toolset>gcc:<optimization>space
157157
<toolset>clang:<inlining>on
158158
<toolset>clang:<optimization>space
159-
<library>/boost/serialization//boost_serialization/<warnings>off ;
159+
<library>/boost/serialization//boost_serialization/<warnings>off
160+
<library>/boost/container//boost_container/<warnings-as-errors>off
161+
<toolset>gcc,<target-os>windows:<build>no ; # Boost.Atomic no longer supports MinGW
160162

161163
compile-fail unordered/insert_node_type_fail.cpp : <define>UNORDERED_TEST_MAP : insert_node_type_fail_map ;
162164
compile-fail unordered/insert_node_type_fail.cpp : <define>UNORDERED_TEST_MULTIMAP : insert_node_type_fail_multimap ;
@@ -266,6 +268,8 @@ run unordered/serialization_tests.cpp
266268
<toolset>clang:<inlining>on
267269
<toolset>clang:<optimization>space
268270
<library>/boost/serialization//boost_serialization/<warnings>off
271+
<library>/boost/container//boost_container/<warnings-as-errors>off
272+
<toolset>gcc,<target-os>windows:<build>no # Boost.Atomic no longer supports MinGW
269273
: foa_serialization_tests ;
270274

271275
local FOA_EXCEPTION_TESTS =
@@ -402,6 +406,10 @@ run cfoa/serialization_tests.cpp
402406
<toolset>clang:<inlining>on
403407
<toolset>clang:<optimization>space
404408
<library>/boost/serialization//boost_serialization/<warnings>off
409+
<library>/boost/container//boost_container/<warnings-as-errors>off
410+
<toolset>gcc,<target-os>windows:<build>no # Boost.Atomic no longer supports MinGW
411+
<toolset>gcc,<thread-sanitizer>norecover:<build>no # TSAN does not support atomic_thread_fence
412+
<toolset>clang,<thread-sanitizer>norecover:<build>no # idem
405413
: cfoa_serialization_tests ;
406414

407415
rule make_cfoa_interprocess_concurrency_tests ( name : defines ? )

test/unordered/erase_if.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2021-2022 Christian Mazakas.
2+
// Copyright 2025 Joaquin M Lopez Munoz.
23
// Distributed under the Boost Software License, Version 1.0. (See accompanying
34
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
45

@@ -42,8 +43,35 @@ namespace test {
4243
}
4344
};
4445

46+
template <class T>
47+
struct non_const_pred;
48+
49+
template<class T, class U>
50+
struct non_const_pred<std::pair<const T, U> >
51+
{
52+
bool operator()(std::pair<const T, U>& x) const
53+
{
54+
U u = std::move(x.second);
55+
(void)u;
56+
return true;
57+
}
58+
};
59+
4560
} // namespace test
4661

62+
template <class UnorderedMap> void test_map_nonconst_erase_if()
63+
{
64+
typedef UnorderedMap map_type;
65+
typedef typename map_type::value_type value_type;
66+
typedef typename map_type::size_type size_type;
67+
68+
map_type m;
69+
m.insert(value_type());
70+
size_type num_erased = erase_if(m, test::non_const_pred<value_type>());
71+
BOOST_TEST(m.empty());
72+
BOOST_TEST_EQ(num_erased, 1u);
73+
}
74+
4775
template <class UnorderedMap> void test_map_erase_if()
4876
{
4977
typedef UnorderedMap map_type;
@@ -71,6 +99,8 @@ template <class UnorderedMap> void test_map_erase_if()
7199
num_erased = erase_if(map, test::is_even());
72100
BOOST_TEST_EQ(map.size(), 2u);
73101
BOOST_TEST_EQ(num_erased, size - map.size());
102+
103+
test_map_nonconst_erase_if<map_type>();
74104
}
75105

76106
template <class UnorderedSet> void test_set_erase_if()

0 commit comments

Comments
 (0)